2013-06-04 52 views
0

whoaa 我真的需要帮助,为什么我的代码结果是这样的? 这是我的代码:android.os.NetworkOnMainThreadException android调用webservice

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()); 
     } 

我也开始加入网络的权限

请帮我 如何提高我的代码,所以这个问题就迎刃而解了。

+0

其N/W在主线程异常转向的asynctasks内部网络的任务就在后台将通过删除严格的模式策略告诉你可以摆脱异常此异常from3.o设备,但它能够更好地移动到异步任务 –

+2

挂上! *让我为你的谷歌!* http://bit.ly/18KwgKT – Swayam

回答

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(); 
+0

我希望有任何例子 – yozawiratama

+0

这[教程](http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html)是非常好...我是从同一个人那里学到的。 – amalBit

3

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

按照doc

当应用程序试图在其主线程执行 网络操作时引发的异常。

这仅适用于针对Honeycomb SDK或 的应用程序。针对早期SDK版本的应用程序允许在其主要事件循环线程上执行联网,但不鼓励 。

0

NetworkOnMainThreadException:当应用程序试图在其主线程进行网络操作时引发的异常。

在Android开发人员网站上有一篇关于Painless Threading的文章,对此进行了很好的介绍,并且将为您提供比实际提供的更好的深度here

AsyncTask中运行您的代码。

您可以通过很好的示例了解关于asyncTask here的最佳解释。

0

调用内部aynctask你的web服务。

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

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




} 
相关问题