Skip to content Skip to sidebar Skip to footer

Httpurlconnection Working On Api Level < 11, Not Working On Api Level >11 On Android

First of all I have two things that need your attention: 1) I tried this code on phones with api level lower than 11 and phones that are pretty high, like 4.3 or 4.4. The high api

Solution 1:

You need to implement all the Network Calls in background Thread via AsyncTask. Here is a dummy template, you can modify it according to your needs:

privateclassLongOperationextendsAsyncTask<String, Void, String> {

    @OverrideprotectedStringdoInBackground(String... params) {
        //Send your HTTP REQUESTS HERE.return"";
    }

    @OverrideprotectedvoidonPostExecute(String result) {
        //UPDATE YOUR UI HERE AFTER RETRIEVING DATA FROM HTTP REQUEST
    }

    @OverrideprotectedvoidonPreExecute() {}

    @OverrideprotectedvoidonProgressUpdate(Void... values) {}
}
}

You need to call this AsyncTask, like this: new LongOperation().execute(); I hope this helps.

Solution 2:

A very common mistake - is a trying call a httpGetURLConnection in main UI thread, cause it is a very heavy operation and UI mustn't "get frozen" while you've work with httpGetURLConnection , so that why in new android versions(after honeyComb) its totaly deprecated. You should use an asyncTask for working with httpGetURLConnection.

Solution 3:

Probably you are getting NetworkOnMainThreadException connection error.

So best way is to use AsyncTask for that.

For more details go with this

Post a Comment for "Httpurlconnection Working On Api Level < 11, Not Working On Api Level >11 On Android"