android.os.NetworkOnMainThreadException android调用webservice

时间:2013-06-04 04:55:36

标签: java android http

whoaa 我真的需要帮助,为什么我的代码结果如此? 这是我的代码:

HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(URL);    
try{
            HttpResponse response = httpClient.execute(httpPost);
            String jsonResult = convertStreamToString((response.getEntity().getContent())).toString();
            JSONObject obj = (JSONObject) new JSONTokener(jsonResult).nextValue();
            JSONObject obj2 = obj.getJSONObject("GetRingkasObjekPajak_SingleResult");
            String nameWP = obj2.getString("NM_WP");
            TextView tv = (TextView)findViewById(R.id.dummy_text_three);
            tv.setText(jsonResult);
        }catch (MalformedURLException e) {
            // URL is invalid
            TextView tv = (TextView) findViewById(R.id.dummy_text_three);
            tv.setText("url invalid");
        } catch (SocketTimeoutException e) {
            // data retrieval or connection timed out
            TextView tv = (TextView) findViewById(R.id.dummy_text_three);
            tv.setText("RTO");
        } catch (IOException e) {
            // could not read response body 
            // (could not create input stream)
            TextView tv = (TextView) findViewById(R.id.dummy_text_three);
            tv.setText("couldnt read response");
        } catch (JSONException e) {
            // response body is no valid JSON string
            TextView tv = (TextView) findViewById(R.id.dummy_text_three);
            tv.setText("json response fail");
        }catch (Exception e) {
            // TODO: handle exception
            TextView tv = (TextView) findViewById(R.id.dummy_text_three);
            tv.setText(e.toString());
        }

我还添加了互联网许可

请帮帮我 如何改进我的代码,这个问题就解决了。

4 个答案:

答案 0 :(得分:3)

您无法从主UI线程执行网络操作。你需要在不同的线程中做网络相关的任务。更好地使用AsyncTask

根据doc

  

应用程序尝试执行时抛出的异常   在其主线程上进行网络操作。

     

这仅适用于针对Honeycomb SDK或。的应用程序   更高。允许使用早期SDK版本的应用程序   他们的主要事件循环线程上的网络,但它是很重要的   气馁。

答案 1 :(得分:3)

以下是异步任务的示例...希望它对您有所帮助。

  private class YourAsyncTaskClass extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {
           //your http network call here.
            return null;
      }      

      @Override
      protected void onPostExecute(String result) {
           //update your ui here
      }

      @Override
      protected void onPreExecute() {
             //do any code before exec
      }

      @Override
      protected void onProgressUpdate(Void... values) {
              //If you want to update a progress bar ..do it here
      }
}   

最后从你想要的任何地方调用这个类..

  new YourAsyncTaskClass().execute();

答案 2 :(得分:0)

NetworkOnMainThreadException :应用程序尝试在其主线程上执行网络操作时引发的异常。

Android开发者网站上有一篇关于Painless Threading的文章,这是对此的一个很好的介绍,它将为您提供比实际提供的here更好的答案。

AsyncTask中运行您的代码。

你可以通过很好的例子了解asyncTask here是最好的解释。

答案 3 :(得分:0)

在asynctask中调用您的Web服务。

private class NetworkTask extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {
         // call your network operation here
      }      




} 
相关问题