android得到响应错误

时间:2012-08-01 10:12:30

标签: java android

在我的申请中,请求没有给我任何回复,我无法弄清楚为什么。这是我的代码。

public static String getResponse(final HttpUriRequest request, final int successCode, final boolean requiresResponse) throws Exception {


        final HttpClient client = getHttpClient();
        System.out.println("getResponce4="+client);
        final HttpResponse response = client.execute(request);
        System.out.println("getResponce5");
        final HttpEntity entity = response.getEntity();
        System.out.println("getResponce6");

        final StringBuilder sb = new StringBuilder();
        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");
        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.9/getUser");

    return getResponse(get, 200, true);
}

我在System.out.println的帮助下检查了,之后我看到了(最后的HttpResponse响应= client.execute(请求);)它不能继续工作。的System.out.println( “getResponce5”);已经不打印了。我不明白这里有什么问题。

我发现了异常

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)

1 个答案:

答案 0 :(得分:0)

请勿使用System.out.println(,而是使用Log.v(tag, text);

有关Log类的更多信息:http://developer.android.com/reference/android/util/Log.html