简体   繁体   English

Android中的Json和ListView崩溃

[英]Json and ListView in android crashes

i'm trying to load a json array into a ViewList, but i cant load the app because op a error, 我正在尝试将json数组加载到ViewList中,但是由于操作错误,我无法加载应用程序,

JSONParser.java JSONParser.java

package app.tabsample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

public class JSONParser extends ListActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactspage);

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.personallist()));
    }

    public ArrayList<String> personallist() {

        ArrayList<String> listItems = new ArrayList<String>();

        try {

            URL jsonurl = new URL("http://izra.co.il/z.php");



            URLConnection transport = jsonurl.openConnection();


            BufferedReader in = new BufferedReader(new InputStreamReader(
                    transport.getInputStream()));


            String line;



            while ((line = in.readLine()) != null) {
                JSONObject obj = new JSONObject(line);
                JSONArray ja = obj.getJSONArray("users");
                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    listItems.add(jo.getString("name"));
                    Log.v("got name",jo.getString("name"));
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();// Catch URL Error
        } catch (IOException e) {
            e.printStackTrace();// Catch IO Exception Error
        } catch (JSONException e) {
            e.printStackTrace(); // Catch JSON Exception Error
        }
        return listItems;
    }
}

contactpage.xml contactpage.xml

  <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />


</LinearLayout>

some how it stucks in those lines: 一些如何卡在这些行中的:

        URLConnection transport = jsonurl.openConnection();

        BufferedReader in = new BufferedReader(new InputStreamReader(
                transport.getInputStream()));

the error: 错误:

05-26 15:39:08.902: E/AndroidRuntime(2931): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.tabsample/app.tabsample.JSONParser}: android.os.NetworkOnMainThreadException

hope someone can help me, i'm whacked... 希望有人可以帮助我,我很沮丧...

thanks alot. 非常感谢。

NetworkOnMainThreadException thrown when an application attempts to perform a networking operation on its main thread. 当应用程序尝试在其主线程上执行联网操作时,引发NetworkOnMainThreadException

so you can use runonuithread , handler , handlerthread or AsyncTask in your Activity for downloading json from sever. 所以你可以使用runonuithread处理程序handlerthread的AsyncTask您的活动从服务器下载JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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