使用HttpResponse时的异常response = client.execute(request);

时间:2015-03-10 20:10:59

标签: android exception httpresponse android-debug

我正在尝试和这个人here做同样的事情,我正在使用AsyncTask来调用executeHttpPost。但是,通过调试应用程序,当它到达行

HttpResponse response = client.execute(request);

它跳过所有内容

if (in != null) {

我很确定网址正常运行。

public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpPost request = new HttpPost(url);
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(formEntity);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        String result = sb.toString();
        return result;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }}}}
            public static String executeHttpGet(String url) throws Exception {
                BufferedReader in = null;
                try {
                    HttpClient client = getHttpClient();
                    HttpGet request = new HttpGet();
                    request.setURI(new URI(url));
                    HttpResponse response = client.execute(request);
                    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    StringBuffer sb     = new StringBuffer("");
                    String line = "";
                    String NL = System.getProperty("line.separator");
                    while ((line = in.readLine()) != null) {
                        sb.append(line + NL);
                    }
                    in.close();
                    String result = sb.toString();
                    return result;
                } finally {
                    if (in != null) {
                        try {   
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }}}}}

logcat的:

W/ActivityThread﹕ Application is waiting for the debugger on port 8100...
I/System.out﹕ Sending WAIT chunk
I/art﹕ Debugger is active
Debugger has connected
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ debugger has settled (1496)
I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
I/OpenGLRenderer﹕ Initialized EGL, version 1.4
I/Choreographer﹕ Skipped 429 frames!  The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 38 frames!  The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 37 frames!  The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 30 frames!  The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 703 frames!  The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 3307 frames!  The application may be doing too much work on its main thread.

1 个答案:

答案 0 :(得分:1)

问题解决了,我添加了权限

<uses-permission android:name="android.permission.INTERNET" />

到AndroidManifest.xml