简体   繁体   中英

Parse JSON data into Android ListView

I've been lurking this site for quite some time now, fortunately until now all my questions have been answered. I was hoping some of you could shed some light on my problem here. I can successfully get a ListView to display an array of strings -- but when I try to parse JSON data into an array -- Well my program just force closes and LogCat doesn't help me much. Anywho -- here is the code I'm using.

String[] journals = {"Filler"};
String result;
JSONArray jArray[];
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try{
  //http post
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx/query.php");
            org.apache.http.HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();


  //convert response to string
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();

            result=sb.toString();

   //I think the problem is somewhere here ....
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    journals[i] = json_data.getString("JournalName");
                    Log.i("log_tag","JournalID: "+json_data.getInt("JournalID") +
                            ",JournalName: " + json_data.getString("JournalName"));
            }
    }
    catch (Exception e) {
    }

    //Or here -- I know this code should REALLY be separated and not in onCreate -- I feel like that's half my problem
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, journals));
    ListView lv = getListView();

Thanks in advance guys! I appreciate it.

03-07 22:24:33.060: ERROR/AndroidRuntime(7986): FATAL EXCEPTION: main
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): java.lang.RuntimeException: Unable to start activity ComponentInfo{Andrew.Badura.A4A.com/Andrew.Badura.A4A.com.SurvAcademia}: java.lang.RuntimeException: Unable to start activity ComponentInfo{Andrew.Badura.A4A.com/Andrew.Badura.A4A.com.JournalActivity}: java.lang.NullPointerException
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.os.Looper.loop(Looper.java:123)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at java.lang.reflect.Method.invoke(Method.java:521)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at dalvik.system.NativeStart.main(Native Method)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{Andrew.Badura.A4A.com/Andrew.Badura.A4A.com.JournalActivity}: java.lang.NullPointerException
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.widget.TabHost.addTab(TabHost.java:216)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at Andrew.Badura.A4A.com.SurvAcademia.onCreate(SurvAcademia.java:25)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     ... 11 more
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): Caused by: java.lang.NullPointerException
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at java.util.Arrays$ArrayList.<init>(Arrays.java:49)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at java.util.Arrays.asList(Arrays.java:171)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at Andrew.Badura.A4A.com.JournalActivity.onCreate(JournalActivity.java:69)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

This code is wrong on so many levels starting with disregard of Java conventions and completely swallowed Exception handling.

I would strongly advise on executing any sort of remote calls in the main thread of your app. You need to wrap it at least into AsyncTask (or possibly Service) and populate your ListView using a callback to make sure that you are back to the main UI thread

你可以使用一个开放的API Gson.jar从Json获取数据。它以标准化的方式获取数据...试试它...例如http://blog.foos-bar.com/2010/08/parsing -facebook-json.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM