Michael Abdelsayed
Michael Abdelsayed

Reputation: 3

Default HTTP request is not working on Android 4.0.4

I'm doing an HTTP request in an Android application using this code:

    try 
    {
        HttpPost post = new HttpPost(<URL>);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        String responseText = EntityUtils.toString(entity);

        parseResponce(responseText);
    } 
    catch (Exception e) 
    {
        Log.e("http post error : ", e.getMessage());
    }

The request is working on android 2.3, but I'm not receiving a response in Android 4.0.4. Why not?

Upvotes: 0

Views: 846

Answers (2)

Alex Lockwood
Alex Lockwood

Reputation: 83311

Take a look at this blog post.

The problem is most likely that you are performing a potentially expensive operation on the main UI thread. There's no way of knowing how long the HTTP request could take, so you should offload the work to a background thread using an AsyncTask, for example.

The reason why this works on Gingerbread but not on ICS is because Android 4.0 is much stricter about abuse against the UI thread (and it will go as far as to crash your application if you abuse it too much).

Upvotes: 1

user468311
user468311

Reputation:

I guess you're having troubles because you perform network operation on UI thread. Try using AsyncTask or Java threading framework.

Upvotes: 0

Related Questions