我正在尝试发出http请求。
我正在使用HTTP解析。
以下是代码
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost("url");
//array NameValuePair;
// Building post parameters, key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
nameValuePair.add(new BasicNameValuePair("id", "2222"));
nameValuePair.add(new BasicNameValuePair("text", "text"));
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
我收到运行时错误。下面是Logcat。
它会产生一些致命异常。
07-08 17:17:37.866: E/AndroidRuntime(26380): FATAL EXCEPTION: main
07-08 17:17:37.866: E/AndroidRuntime(26380): android.os.NetworkOnMainThreadException
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
07-08 17:17:37.866: E/AndroidRuntime(26380): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-08 17:17:37.866: E/AndroidRuntime(26380): at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
07-08 17:17:37.866: E/AndroidRuntime(26380): at libcore.io.IoBridge.connect(IoBridge.java:112)
07-08 17:17:37.866: E/AndroidRuntime(26380): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-08 17:17:37.866: E/AndroidRuntime(26380): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-08 17:17:37.866: E/AndroidRuntime(26380): at java.net.Socket.connect(Socket.java:857)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-08 17:17:37.866: E/AndroidRuntime(26380): at com.custom.functions.citycoupons.CustomDialog$7.onClick(CustomDialog.java:465)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.view.View.performClick(View.java:4091)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.view.View$PerformClick.run(View.java:17072)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.os.Handler.handleCallback(Handler.java:615)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.os.Looper.loop(Looper.java:153)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.app.ActivityThread.main(ActivityThread.java:5086)
07-08 17:17:37.866: E/AndroidRuntime(26380): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 17:17:37.866: E/AndroidRuntime(26380): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 17:17:37.866: E/AndroidRuntime(26380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
07-08 17:17:37.866: E/AndroidRuntime(26380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
07-08 17:17:37.866: E/AndroidRuntime(26380): at dalvik.system.NativeStart.main(Native Method)
07-08 17:17:39.336: E/Trace(26551): error opening trace file: No such file or directory (2)
如何解决此问题? 任何帮助将不胜感激。 提前谢谢。
答案 0 :(得分:1)
您无法在UIThread中发出HTTP请求。使用AsyncTask或其他线程。
new AsyncTask<Void, Void, Void>() {
String token;
@Override
protected Void doInBackground(Void... params) {
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost("url");
//array NameValuePair;
// Building post parameters, key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
nameValuePair.add(new BasicNameValuePair("id", "2222"));
nameValuePair.add(new BasicNameValuePair("text", "text"));
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}.execute();
答案 1 :(得分:0)
使用Async Task发送HTTP请求。当你在主线程上发出请求时,它会在你的粘滞模式上给你NetworkOnMainThreadException
。
答案 2 :(得分:0)
在2.2之后,您无法在主要网络上调用网络操作,这就是它显示NetworkOnMainThreadException
的原因。要解决此问题,请使用AsyncTask类
例如,
从主线程调用,即在setContentView(),
之后new LongTask(your_activity.this).execute();
private class LongTask extends AsyncTask<Void, Void, String>
{
protected void doInBackground(Void... params) // inside this method do your network operations
{
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost("url");
//array NameValuePair;
// Building post parameters, key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
nameValuePair.add(new BasicNameValuePair("id", "2222"));
nameValuePair.add(new BasicNameValuePair("text", "text"));
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
return response.toString();
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
return "Error"
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
return "Error"
}
}
protected void onPostExecute(String s)
{
super.onPostExecute(s);
// here with String "s", you can access the result you got from service
}
}
请检查AsyncTask