Skip to content Skip to sidebar Skip to footer

Getting An Exception While Using Httpresponse Response = Client.execute(request);

I'm trying to request the response from server, but when I use 'HttpResponse response = client.execute(request);', program enters the exception case. Here is my code: function for

Solution 1:

If you're building against any version of Android >= Honeycomb you cannot make network calls on the main thread. Try putting this in an Async Task and see if it works.

Solution 2:

The answer of dell116 are right.

I had the same problem on ICS and solve it asynchronously with this code:

privatevoidgetResponseThread(final String url) {
    newThread(newRunnable() {
        publicvoidrun() {
            String cadHTTP = getResponse(url);
            Message msg = newMessage();
            msg.obj = cadHTTP;
            handlerHTTP.sendMessage(msg);
        }
    }).start();
}

privateStringgetResponse(String url) {
    HttpClient httpClient = newDefaultHttpClient();
    HttpGet del = newHttpGet(url);
    del.setHeader("content-type", "application/json");

    String respStr;
    try {
        HttpResponse resp = httpClient.execute(del);
        respStr = EntityUtils.toString(resp.getEntity());
    } catch(Exception ex) {
        Log.e("RestService","Error!", ex);
        respStr = "";
    }

    Log.e("getResponse",respStr);
    return respStr;
}

privateHandler handlerHTTP = newHandler() {
    @OverridepublicvoidhandleMessage(Message msg) {
        String res = (String) msg.obj;
        //CONTINUE HEREnexTask(res);
    }
};

Regards! :)

Post a Comment for "Getting An Exception While Using Httpresponse Response = Client.execute(request);"