Mark Khateeb
Mark Khateeb

Reputation: 917

exception while using HttpResponse response = client.execute(request);

I am trying to do the same as this guy here and I am using AsyncTask to call the executeHttpPost. However, by debugging the application, when it reaches the line

HttpResponse response = client.execute(request);

it skip everything to the line

if (in != null) {

I am pretty sure that the URL is working.

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.

Upvotes: 1

Views: 1070

Answers (1)

Mark Khateeb
Mark Khateeb

Reputation: 917

Problem solved, I added the permission

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

to AndroidManifest.xml

Upvotes: 1

Related Questions