为什么它不起作用方法线程?

时间:2015-04-10 10:37:11

标签: android

下午好。我是编程新手。那决定学习一些json。写了代码。这需要json并显示文本。但在工作过程中,面对这个

为什么它不起作用方法线程? 我只想显示代码json

    public class MyActivity extends Activity {
    private JSONArray jsonArray;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Thread thread=new Thread(){
            @Override
            public void run() {
                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpGet request = new HttpGet("http://irkvuz.ru/isea/timetable/get/get_schedule.php?group_id=14925");
                    HttpResponse response = client.execute(request);
                    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    String line = "";
                    String str = "";
                    while ((line = rd.readLine()) != null) {
                        str+=line;
                    }
                    jsonArray=new JSONArray(str);
                } catch (JSONException e) {e.printStackTrace();
                } catch (ClientProtocolException e) {e.printStackTrace();
                } catch (IOException e) {e.printStackTrace();}
            }
        };
        thread.start();

        try {
          //EXEPTION THIS IS 
    Log.e("asdasda",jsonArray.get(0).toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}




 Caused by: java.lang.NullPointerException
        at com.example.myapp.MyActivity.onCreate(MyActivity.java:49)
        at android.app.Activity.performCreate(Activity.java:5122)
        at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)

1 个答案:

答案 0 :(得分:-1)

public class MyActivity extends Activity {
    private JSONObject jsonObject;
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Thread thread=new Thread(){
            @Override 
            public void run() { 
                try { 
                    HttpClient client = new DefaultHttpClient();
                    HttpGet request = new HttpGet("http://irkvuz.ru/isea/timetable/get/get_schedule.php?group_id=14925");
                    HttpResponse response = client.execute(request);
                    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    String line = "";
                    String str = "";
                    while ((line = rd.readLine()) != null) {
                        str+=line;
                    } 
                    jsonObject = new JSONObject(str);
                    runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                     TextView textView= (TextView) findViewById(R.id.textView);
                    textView.setText(jsonArray.toString());
                    }
                    });


                } catch (JSONException e) {e.printStackTrace();
                } catch (ClientProtocolException e) {e.printStackTrace();
                } catch (IOException e) {e.printStackTrace();}
            } 
        }; 

        thread.start();
    } 
}