在Android中读取json url文件会在我的手机上返回空白屏幕 - 手机上不显示任何内容



这是我在Stackoverflow上的第一篇文章,我正在努力学习Android。我需要从android应用程序中读取一个json文件。

I ran the following code on my phone and it doesn't display anything on my phone, I just get a blank screen.

我知道有一些方法可以使用Java库来解析json,但这不是我想要的。我想使用Android提供的ONLY工具来完成对这个json的阅读。这是我的json

我正在使用Android 4.0.3

json文件数据:

{
    "offers": [
        {
                "id": "1",
                "type":"coupon",
                "description":"half off at the GAP",
                "url":"http://www.gap.com/"
        },
        {
                "id": "1",
                "type":"offer",
                "description":"buy one get one at Ann Taylor",
                "url":"http://www.anntaylor.com/"
        },
        {
                "id": "1",
                "type":"type:promotion",
                "description":"free kids scoop for first ten people",
                "url":"http://www.gap.com/"
        }
    ]
}

MainActivity.java

package com.pega.parsejson;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
    TextView jsonWrapper;
    HttpClient httpClient;
    JSONObject json;

    final static String offersUrl="http://somewebsite.com/offers.json";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        jsonWrapper=(TextView) findViewById(R.id.jsonWrap);
        httpClient=new DefaultHttpClient();
        new Read().execute("description");
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public JSONObject jsonOffers() throws ClientProtocolException, IOException, JSONException {
        //StringBuilder url=new StringBuilder(offersUrl);
        HttpGet get=new HttpGet(offersUrl);
        HttpResponse res=httpClient.execute(get);
        int status=res.getStatusLine().getStatusCode();
        if(status==200){
            HttpEntity e=res.getEntity();
            String data=EntityUtils.toString(e);
            JSONArray timeline=new JSONArray("offers");
            JSONObject jsonOffer=timeline.getJSONObject(0); //returns most recent offer
            return jsonOffer;
        }else{
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
            return null;
        }
    }
    public class Read extends AsyncTask<String, Integer, String>{
        @Override
        protected String doInBackground(String... params) {
            try {
                json=jsonOffers();
                return (String) json.get("description"); //returns string w/ parameter passed in as "description"
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            jsonWrapper.setText(result);
        }
    }
}

我也在使用互联网许可证:

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pega.parsejson"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pega.parsejson.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/jsonWrap"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:text="" />
</RelativeLayout>

logcat

[2013-08-07 23:35:41 - parseJson] ------------------------------
[2013-08-07 23:35:41 - parseJson] Android Launch!
[2013-08-07 23:35:41 - parseJson] adb is running normally.
[2013-08-07 23:35:41 - parseJson] Performing com.pega.parsejson.MainActivity activity launch
[2013-08-07 23:35:42 - parseJson] Uploading parseJson.apk onto device 'HT25EHX00690'
[2013-08-07 23:35:42 - parseJson] Installing parseJson.apk...
[2013-08-07 23:35:45 - parseJson] Success!
[2013-08-07 23:35:45 - parseJson] Starting activity com.pega.parsejson.MainActivity on device HT25EHX00690
[2013-08-07 23:35:46 - parseJson] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.pega.parsejson/.MainActivity }



请注意,我不想使用任何json库来解析我的json,我严格希望只使用android来解析json。我只是不明白为什么我的主Activity.java在手机上什么都不显示。

很抱歉我的帖子太长了,对于一个时间敏感的项目,我真的需要帮助,是的,我已经在Stackoverflow上读过其他类似的问题,但似乎没有人回答这个问题

有人能帮我吗?

感谢

试试这样的东西:

JSONObject jObj;
JSONArray data = null;
JSONObject jsonOffer;
    jObj = new JSONObject(data);
    data = jObj.getJSONArray("offers");
    jsonOffer=data.getJSONObject(0);

希望它有帮助!!

最新更新