HttpRequest class using AsyncTask for Android application I am using the following class to retrieve a http response, which contains data for my Android App. Everything works fine, but there is a performance issue. I get the following warning when requesting this class:


  Skipped 36 frames!  The application may be doing too much work on its main thread.


I used an AsyncTask to run the network connection on a different thread, but it is still saying I'm doing too much work on the main thread. Is there something you can point out to me that is slowing down my program?

package com.lakshan.gamematch.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

public class HttpRequest {

String url_to_task = "";
String string_result = "hasn't returned a value";

        public String getFromURL (String url) {
            url_to_task = url;
            new MyTask().execute();
            while (string_result.equals("hasn't returned a value")) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return string_result;
        }

        private class MyTask extends AsyncTask &lt;Void, Void, Void&gt; {
            @Override
            protected Void doInBackground(Void... params) {
                InputStream is = null;

                //Download JSON data from url
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(url_to_task);
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                    Log.e("entity", entity.getContent().toString());

                } catch (Exception e) {
                    Log.e("log_tag", "Error in http connection " + e.toString());
                }

                //Convert response to string
                try {
                    BufferedReader reader = new BufferedReader (new InputStreamReader(is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");

                    }
                    is.close();
                    string_result = sb.toString();
                } catch (Exception e) {
                    Log.e("log_tag", "Error converting result" + e.toString());
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {             
                super.onPostExecute(result);
            }
        }
}
By default AsyncTask tasks execute sirially on the default executor and you should at least override the execute to make sure that all requests are run in parallel unless specified otherwise
Apache HTTP library is considered deprecated in Android and you should try to at least use HttpUrlConnection if not a library that can make this happen with zero boilerplate code sych as Volley, OkHttp or Retrofit
Using Thread.sleep on the main thread is the best way you have to cause these warnings.
The system has about 16.8 milliseconds to draw anything on the screen and you just took away 10 of them for nothing while blocking the system from using the thread.
please use the given onPreExecute and onPostExecute to show progress activity to the user in any way you see fit and never EVER sleep on the main thread