当我从我的Activity调用HttpRequests类时,它会抛出NetworkOnMainThreadException,我无法理解为什么会这样做。我把所有网络处理都放在doInBackground方法中。有什么想法吗?
我的AsyncTask:
public class HttpRequests extends AsyncTask<Void, Void, String>{
private List<NameValuePair> nameValuePairs;
private String siteUrl;
public HttpRequests(List<NameValuePair> nameValuePairs, String siteUrl){
this.nameValuePairs = nameValuePairs;
this.siteUrl = siteUrl;
}
@Override
protected String doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(siteUrl);
try {
if (null != nameValuePairs)
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
response = httpclient.execute(httppost);
String responseStr = EntityUtils.toString(response.getEntity());
return responseStr;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return "";
}
}
MainActivity中的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HttpRequests request = new HttpRequests(null, "http://google.com");
request.execute();
}
抛出:
03-22 11:32:43.527 2516-2516/quiz.com.quizapp D/AndroidRuntime﹕ Shutting down VM
03-22 11:32:43.527 2516-2516/quiz.com.quizapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: quiz.com.quizapp, PID: 2516
java.lang.RuntimeException: Unable to start activity ComponentInfo{quiz.com.quizapp/quiz.com.quizapp.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at quiz.com.quizapp.HttpRequests.makePostHttpRequest(HttpRequests.java:45)
at quiz.com.quizapp.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
答案 0 :(得分:1)
从Asynctask扩展你的类,如下所示,
HttpRequests extends AsyncTask<List<NameValuePair> namValuePairs, Void, String> {
public void setUrl(String url) {
//assign to request
}
@Override
protected JSONObject doInBackground(List<NameValuePair>... params) {
//your logic
return return responseStr;
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}