How to fix 'android.os.NetworkOnMainThreadException'?
(56个答案)
5年前关闭。
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.util.Log;
public class GetPostEx {
public String getInternetData() throws Exception{
//String source_prog;
//int lang_number;
//BufferedReader in = null;
String ret=null;
try{
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("Some server link");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("source", "print 1"));
nameValuePairs.add(new BasicNameValuePair("lang", "5"));
nameValuePairs.add(new BasicNameValuePair("testcases", "[\"1\"]"));
nameValuePairs.add(new BasicNameValuePair("api_key", "Some key"));
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.e("error", "before HTTPResponse");
HttpResponse response = client.execute(request);
Log.e("error", "Aftr HTTPResponse");
ret = EntityUtils.toString(response.getEntity());
}catch (Exception e){
//Log.e("error","error!!");
e.printStackTrace();
}
return ret;
}
}
和其他活动(AndroidHTTPRequestsActivity)中使用的代码(我从中调用GetPostEx类的位置)是:
GetPostEx test = new GetPostEx();
try{
returned = test.getInternetData();
} catch(Exception e){
Log.e("error", "yo") ;
e.printStackTrace();
}
我正在尝试制作一个需要使用API从服务器进行HTTP响应的android应用程序。这是获取HTTP响应的代码。但是在HttpResponse response = client.execute(request)行之后,执行暂停在此行之后,logcat未显示错误,而上面的一个正在显示。
我无法找出背后的原因。
以下几行是logcat中的堆栈跟踪详细信息:
12-22 14:18:58.658:E /错误(1335):在HTTPResponse之前
12-22 14:18:59.448:W / System.err(1335):android.os.NetworkOnMainThreadException
12-22 14:18:59.448:W / System.err(1335):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
12-22 14:18:59.458:W / System.err(1335):位于java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-22 14:18:59.458:W / System.err(1335):位于java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-22 14:18:59.458:W / System.err(1335):位于java.net.InetAddress.getAllByName(InetAddress.java:214)
12-22 14:18:59.458:W / System.err(1335):位于org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
12-22 14:18:59.458:W / System.err(1335):位于org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-22 14:18:59.458:W / System.err(1335):位于org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-22 14:18:59.478:W / System.err(1335):位于org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-22 14:18:59.478:W / System.err(1335):位于org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-22 14:18:59.478:W / System.err(1335):位于org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-22 14:18:59.478:W / System.err(1335):位于org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-22 14:18:59.478:W / System.err(1335):at com.us.onlinecompilr.GetPostEx.getInternetData(GetPostEx.java:37)
12-22 14:18:59.478:W / System.err(1335):at com.us.onlinecompilr.AndroidHTTPRequestsActivity.onCreate(AndroidHTTPRequestsActivity.java:29)
12-22 14:18:59.478:W / System.err(1335):在android.app.Activity.performCreate(Activity.java:5243)
12-22 14:18:59.478:W / System.err(1335):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-22 14:18:59.478:W / System.err(1335):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-22 14:18:59.488:W / System.err(1335):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-22 14:18:59.488:W / System.err(1335):at android.app.ActivityThread.access $ 700(ActivityThread.java:135)
12-22 14:18:59.488:W / System.err(1335):位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1397)
12-22 14:18:59.488:W / System.err(1335):位于android.os.Handler.dispatchMessage(Handler.java:102)
12-22 14:18:59.488:W / System.err(1335):位于android.os.Looper.loop(Looper.java:137)
12-22 14:18:59.488:W / System.err(1335):位于android.app.ActivityThread.main(ActivityThread.java:4998)
12-22 14:18:59.488:W / System.err(1335):at java.lang.reflect.Method.invokeNative(本机方法)
12-22 14:18:59.488:W / System.err(1335):at java.lang.reflect.Method.invoke(Method.java:515)
12-22 14:18:59.488:W / System.err(1335):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:777)
12-22 14:18:59.488:W / System.err(1335):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-22 14:18:59.488:W / System.err(1335):在dalvik.system.NativeStart.main(本机方法)
12-22 14:18:59.588:I / Choreographer(1335):跳过641帧!该应用程序可能在其主线程上做太多工作。
12-22 14:19:03.278:I / Choreographer(1335):跳过了33帧!该应用程序可能在其主线程上做太多工作。
12-22 14:19:06.188:I / Choreographer(1335):跳过112帧!该应用程序可能在其主线程上做太多工作。
我是一名优秀的程序员,十分优秀!