-1

我尝试从 php 文件中获取 JSON 数据

public void connect(){
    System.out.println("%%%%%%%%%%%%%%%%%1" );
    Thread t = new Thread(){

        @Override
        public void run() {

    try {

        System.out.println("%%%%%%%%%%%%%%%%%2" );
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setSoTimeout(params, 0);
        HttpClient httpClient = new DefaultHttpClient(params);
        String urlString = "http://url";
        //prepare the HTTP GET call 
        HttpGet httpget = new HttpGet(urlString);
        //get the response entity
        HttpEntity entity = httpClient.execute(httpget).getEntity();
        System.out.println("%%%%%%%%%%%%%%%%%3" );
        if (entity != null) {
            //get the response content as a string
            String response = EntityUtils.toString(entity);
            //consume the entity
            entity.consumeContent();

            // When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
            httpClient.getConnectionManager().shutdown();

            //return the JSON response

            JSONArray jsonarray = new JSONArray(response); 
            JSONObject jb =(JSONObject) jsonarray.getJSONObject(0);
            String name= jb.getString("name");
            String punkt = jb.getString("punktezahl");

             //String name = jsonarray.getString("name");
             System.out.println("HEEEEEEEEEEEEEEEEEEEEEEEEEEEE" + name);
             fuehrender.setText(name);
             punkte.setText(punkt);
               }



    }catch (Exception e) {
        e.printStackTrace();
    }
}

};
t.start();
}

如果我这样做,我会收到一条消息,即只有创建视图层次结构的原始线程才能触及其视图。

因此,由于此错误消息,我这样尝试:

public void connect(){
    System.out.println("%%%%%%%%%%%%%%%%%1" );
    runOnUiThread(new Runnable() {

        @Override
        public void run() {

    try {

        System.out.println("%%%%%%%%%%%%%%%%%2" );
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setSoTimeout(params, 0);
        HttpClient httpClient = new DefaultHttpClient(params);
        String urlString = "http://url";
        //prepare the HTTP GET call 
        HttpGet httpget = new HttpGet(urlString);
        //get the response entity
        HttpEntity entity = httpClient.execute(httpget).getEntity();
        System.out.println("%%%%%%%%%%%%%%%%%3" );
        if (entity != null) {
            //get the response content as a string
            String response = EntityUtils.toString(entity);
            //consume the entity
            entity.consumeContent();

            // When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
            httpClient.getConnectionManager().shutdown();

            //return the JSON response

            JSONArray jsonarray = new JSONArray(response); 
            JSONObject jb =(JSONObject) jsonarray.getJSONObject(0);
            String name= jb.getString("name");
            String punkt = jb.getString("punktezahl");

             //String name = jsonarray.getString("name");
             System.out.println("HEEEEEEEEEEEEEEEEEEEEEEEEEEEE" + name);
             fuehrender.setText(name);
             punkte.setText(punkt);
               }



    }catch (Exception e) {
        e.printStackTrace();
    }
}

});

}
}

现在,我收到NetworkOnMainThread错误消息。如何突破这个厄运循环?

4

1 回答 1

1

你有一个runOnUiThread. 去掉它。它应该只用于更新 ui 而不是用于 http get 请求。

使用AsyncTask是更好的选择。发出 http get 请求doInBackground并解析响应。您可以将doInbackground作为参数的结果返回到onPostExecute.

因此,您可以更新在onPostExecuteui 线程上调用的 ui。

http://developer.android.com/reference/android/os/AsyncTask.html

例子:

调用

new TheTask().execute(); // in ui thread

制作AsyncTask一个内部活动类

class TheTask extends AsyncTask<Void,Void,String>
{

    @Override
    protected String doInBackground(Void... params1) {
        String response = null; 
            try {
                HttpParams params = new BasicHttpParams();
                HttpConnectionParams.setSoTimeout(params, 0);
                HttpClient httpClient = new DefaultHttpClient(params);
                String urlString = "http://url";
                HttpGet httpget = new HttpGet(urlString);
                HttpEntity entity = httpClient.execute(httpget).getEntity();
                response = EntityUtils.toString(entity);
                httpClient.getConnectionManager().shutdown();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
         JSONArray jsonarray = new JSONArray(result; 
         JSONObject jb =(JSONObject) jsonarray.getJSONObject(0);
         String name= jb.getString("name");
         String punkt = jb.getString("punktezahl");
         fuehrender.setText(name);
         punkte.setText(punkt);
    }

}
于 2014-01-24T17:51:27.557 回答