2013-03-15 106 views
0

我开发android应用程序,并且每件事情都是正确的没有错误,但是当我运行代码时,我发现下面这个错误,当我跟踪代码时,它停在这一行:threadid = 1线程退出与未捕获的异常(组= 0x410702a0)

HttpResponse httpResponse = httpClient.execute(httpPost); 

的部分代码:

try { 
     // defaultHttpClient 

     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 
     Log.d("w", "2a json..................."); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     Log.d("w", "2b json..................."); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     Log.d("w", "2c json..................."); 
     is = httpEntity.getContent(); 
     Log.d("w", "3 json..................."); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

错误:

threadid=1: thread exiting with uncaught exception (group=0x410702a0) 
FATAL EXCEPTION: main 
android.os.NetworkOnMainThreadException 
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) 
    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 
    at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 
    at libcore.io.IoBridge.connect(IoBridge.java:112) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 
    at java.net.Socket.connect(Socket.java:842) 
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 
+1

的[android.os.NetworkOnMainThreadException]可能重复(http://stackoverflow.com/questions/6343166/android-os -networkonmainthreadexception) – assylias 2013-03-15 16:22:08

+0

http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html – assylias 2013-03-15 16:22:14

回答

0

创建ñ的AsyncTask并做doInBackground方法中你的网络呼叫

这里是你的代码

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

    private Exception exception; 

    protected void doInBackground(String... urls) { 
     try { 
     // defaultHttpClient 

     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 
     Log.d("w", "2a json..................."); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     Log.d("w", "2b json..................."); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     Log.d("w", "2c json..................."); 
     is = httpEntity.getContent(); 
     Log.d("w", "3 json..................."); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 

    protected void onPostExecute() { 

// write what u want to do after completion of the doInBackground method 
    } 
} 

new ABCTask().execute(); 
相关问题