[英]This thread forbids HTTP requests
in my application I am trying make a requst but i have received an error. 在我的应用程序中,我正在尝试进行请求,但我收到了一个错误。 Here is my code and the exception.
这是我的代码和例外。 I check out my code I saw that the exception coming from final HttpResponse response = client.execute(request);
我查看了我的代码,我看到来自最终HttpResponse的异常响应= client.execute(请求); this line.
这条线。 What is the problem here.
这里有什么问题。
public static String getResponse(final HttpUriRequest request, final int successCode, final boolean requiresResponse) {
final StringBuilder sb = new StringBuilder();
final HttpClient client = getHttpClient();
System.out.println("getResponce4");
try {
final HttpResponse response = client.execute(request);
System.out.println("getResponce5");
final HttpEntity entity = response.getEntity();
System.out.println("getResponce6");
if (entity != null) {
final InputStream stream = entity.getContent();
byte bytes[] = new byte[4096];
int numBytes;
while ((numBytes=stream.read(bytes))!=-1) {
if (numBytes!=0) {
sb.append(new String(bytes, 0, numBytes));
}
}
}
if (response.getStatusLine().getStatusCode() != successCode) {
closeConnection(client);
throw new Exception(sb.toString());
}
System.out.println("getResponce1");
closeConnection(client);
System.out.println("getResponce2");
if (!requiresResponse) return null;
System.out.println("getResponce3");
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return sb.toString();
}
private static void closeConnection(final HttpClient client) {
try {
final Method closeMethod = client.getClass().getMethod("close");
if (closeMethod != null) {
closeMethod.invoke(client);
}
}
catch (IllegalArgumentException ignored) {}
catch (IllegalAccessException ignored) {}
catch (InvocationTargetException ignored) {}
catch (SecurityException ignored) {}
catch (NoSuchMethodException ignored) {}
}
private static HttpClient getHttpClient() {
Class<?> androidClientClass;
try {
androidClientClass = Class.forName("android.net.http.AndroidHttpClient");
final Method method = androidClientClass.getMethod("newInstance", String.class);
return (HttpClient) method.invoke(null, "newInstance");
} catch (ClassNotFoundException ignored) {}
catch (IllegalArgumentException ignored) {}
catch (IllegalAccessException ignored) {}
catch (InvocationTargetException ignored) {}
catch (SecurityException ignored) {}
catch (NoSuchMethodException ignored) {}
return new DefaultHttpClient();
}
private static String notInitialized() {
return "{ \"errorCode\" : 101 }";
}
public static String getAddress(final String key1, final String key2, final String lat, final String lng) throws Exception {
final HttpGet get = new HttpGet("http://192.168.1.4:3000/api/1.0/zoappsgeo/address/lookup");
return getResponse(get, 200, true);
}
exception 例外
08-01 14:44:29.272: WARN/System.err(14718): java.lang.RuntimeException: This thread forbids HTTP requests
08-01 14:44:29.272: WARN/System.err(14718): at android.net.http.AndroidHttpClient$1.process(AndroidHttpClient.java:91)
08-01 14:44:29.272: WARN/System.err(14718): at org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290)
08-01 14:44:29.272: WARN/System.err(14718): at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:160)
08-01 14:44:29.272: WARN/System.err(14718): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
08-01 14:44:29.272: WARN/System.err(14718): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-01 14:44:29.272: WARN/System.err(14718): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-01 14:44:29.272: WARN/System.err(14718): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-01 14:44:29.272: WARN/System.err(14718): at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:243)
08-01 14:44:29.272: WARN/System.err(14718): at com.ZoApps.VTGClient.getResponse(VTGClient.java:59)
08-01 14:44:29.272: WARN/System.err(14718): at com.ZoApps.VTGClient.getAddress(VTGClient.java:139)
08-01 14:44:29.272: WARN/System.err(14718): at com.ZoApps.DataActivity$1.onClick(DataActivity.java:75)
08-01 14:44:29.272: WARN/System.err(14718): at android.view.View.performClick(View.java:2532)
08-01 14:44:29.272: WARN/System.err(14718): at android.view.View$PerformClick.run(View.java:9293)
08-01 14:44:29.272: WARN/System.err(14718): at android.os.Handler.handleCallback(Handler.java:587)
08-01 14:44:29.272: WARN/System.err(14718): at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 14:44:29.272: WARN/System.err(14718): at android.os.Looper.loop(Looper.java:150)
08-01 14:44:29.282: WARN/System.err(14718): at android.app.ActivityThread.main(ActivityThread.java:4277)
08-01 14:44:29.282: WARN/System.err(14718): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 14:44:29.282: WARN/System.err(14718): at java.lang.reflect.Method.invoke(Method.java:507)
08-01 14:44:29.282: WARN/System.err(14718): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-01 14:44:29.282: WARN/System.err(14718): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-01 14:44:29.282: WARN/System.err(14718): at dalvik.system.NativeStart.main(Native Method)
DefaultHttpClient (from the org.apache.http.impl.client package) has been around since Android API level 1. AndroidHttpClient (from the android.net.http.AndroidHttpClient package) has only been available since Android API level 8. DefaultHttpClient(来自org.apache.http.impl.client包)自Android API级别1以来一直存在.AndroidHttpClient(来自android.net.http.AndroidHttpClient包)自Android API级别8以来才可用。
Most importantly, however, is the fact that AndroidHttpClient is not allowed to execute in the main (typically GUI) application thread. 然而,最重要的是,AndroidHttpClient不允许在主(通常是GUI)应用程序线程中执行。
Check more at http://www.intertech.com/Blog/android-defaulthttpclientandroidhttpclient-and-httpparams/ 有关详细信息, 请访问http://www.intertech.com/Blog/android-defaulthttpclientandroidhttpclient-and-httpparams/
I had this proble too. 我也有这个问题。 Change your
改变你的
androidClientClass = Class.forName("android.net.http.AndroidHttpClient");
into 成
androidClientClass = Class.forName("android.net.http.DefaultHttpClient");
And it is done. 它完成了。
The problem is that android does not allow to send requests from the ui thread: 问题是android不允许从ui线程发送请求:
AndroidHttpClient: AndroidHttpClient:
if (Looper.myLooper() != null && Looper.myLooper() == Looper.getMainLooper() ) {
throw new RuntimeException("This thread forbids HTTP requests");
}
You can either use the DefaultHttpClient, or AsyncTask before the request. 您可以在请求之前使用DefaultHttpClient或AsyncTask。
Check also the: http://loopj.com/android-async-http/ lib. 另请查看: http : //loopj.com/android-async-http/ lib。
As a quick solution replace AndroidHttpClient with DefaultHttpClient class. 作为一个快速解决方案,用DefaultHttpClient类替换AndroidHttpClient。 It is not a good solution though but shall work.
虽然这不是一个好的解决方案,但应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.