Android 此线程禁止HTTP请求

Android 此线程禁止HTTP请求,android,httpclient,Android,Httpclient,在我的应用程序中,我正在尝试进行请求,但我收到了一个错误。这是我的代码和例外。我检查了我的代码,发现异常来自最终的HttpResponse response=client.execute(request);这条线。这里的问题是什么 public static String getResponse(final HttpUriRequest request, final int successCode, final boolean requiresResponse) { final

在我的应用程序中,我正在尝试进行请求,但我收到了一个错误。这是我的代码和例外。我检查了我的代码,发现异常来自最终的HttpResponse response=client.execute(request);这条线。这里的问题是什么

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);
    }

作为一种快速解决方案,使用DefaultHttpClient类替换AndroidHttpClient。虽然这不是一个好的解决方案,但应该可以使用。

作为一个快速解决方案,使用DefaultHttpClient类替换AndroidHttpClient。虽然这不是一个好的解决方案,但会奏效。

我也有这个问题。换衣服

androidClientClass = Class.forName("android.net.http.AndroidHttpClient");
进入


就这样了。

我也有这个问题。换衣服

androidClientClass = Class.forName("android.net.http.AndroidHttpClient");
进入


问题是android不允许从ui线程发送请求:

AndroidHttpClient:

if (Looper.myLooper() != null && Looper.myLooper() == Looper.getMainLooper() ) {
         throw new RuntimeException("This thread forbids HTTP requests");
}
您可以在请求之前使用DefaultHttpClient或AsyncTask

另请检查:
lib.

问题在于android不允许从ui线程发送请求:

AndroidHttpClient:

if (Looper.myLooper() != null && Looper.myLooper() == Looper.getMainLooper() ) {
         throw new RuntimeException("This thread forbids HTTP requests");
}
您可以在请求之前使用DefaultHttpClient或AsyncTask

另请检查: lib.

DefaultHttpClient(来自org.apache.http.impl.client包)从Android API级别1开始就存在了。 AndroidHttpClient(来自android.net.http.AndroidHttpClient包)仅在android API级别8之后才可用

然而,最重要的是,不允许在主(通常是GUI)应用程序线程中执行AndroidHttpClient

更多信息,请查看自Android API级别1以来一直存在的DefaultHttpClient(来自org.apache.http.impl.client包)。 AndroidHttpClient(来自android.net.http.AndroidHttpClient包)仅在android API级别8之后才可用

然而,最重要的是,不允许在主(通常是GUI)应用程序线程中执行AndroidHttpClient


查看更多信息我的第一个想法是,你试图在UI线程上执行此请求,这是错误的,因为这样做会阻止你的应用程序。你必须生成一个新线程或一个异步任务来执行此操作。干杯。我的第一个想法是,你试图在UI线程上执行此请求,这是错误的,因为这样做会阻止你的应用程序。你必须生成一个新线程或一个异步任务来执行此操作。干杯。这可能是最好的答案,应该检查为已接受。被接受的答案只是一个解决办法。这可能是最好的答案,应该检查为已接受。被接受的只是一种变通方法。