Properly Using AsyncTask get() Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Properly Using AsyncTask get() without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am running into a problem. I need to use asynctask to retrieve JSON data and I need that data before I moved to the next part of the program. However, when using the get() method of AsyncTask I have 5 to 8 sec black screen before I see the data is displayed. I would like to display a progress dialog during the data retrieval but I cannot do this due to the black screen. Is there a way to put into another thread? here is some code


public class DataResponse extends AsyncTask<String, Integer, Data> {

    AdverData delegate;
    Data datas= new Data();
    Reader reader;
    Context myContext;
    ProgressDialog dialog;
    String temp1;

public DataResponse(Context appcontext) {


protected void onPreExecute()
    dialog= new ProgressDialog(myContext);

    protected Data doInBackground(String... params) {
            InputStream source = retrieveStream(temp1);
            reader = new InputStreamReader(source);
        catch (Exception e)

            Gson gson= new Gson();
            datas= gson.fromJson(reader, Data.class);    

            return datas; 


    protected void onPostExecute(Data data) 



    private InputStream retrieveStream(String url) {

            DefaultHttpClient client = new DefaultHttpClient(); 

            HttpGet getRequest = new HttpGet(url);

            try {

               HttpResponse getResponse = client.execute(getRequest);
               final int statusCode = getResponse.getStatusLine().getStatusCode();

               if (statusCode != HttpStatus.SC_OK) { 
                      "Error " + statusCode + " for URL " + url); 
                  return null;

               HttpEntity getResponseEntity = getResponse.getEntity();
               return getResponseEntity.getContent();

            catch (IOException e) {
               Log.w(getClass().getSimpleName(), "Error for URL " + url, e);

            return null;




public class DisplayInfo extends Activity implements AdverData {

public static Data data;
public ProjectedData attup;
public ProjectedData attdown;
public ProjectedData sprintup;
public ProjectedData sprintdown;
public ProjectedData verizionup;
public ProjectedData veriziondown;
public ProjectedData tmobileup;
public ProjectedData tmobiledown;
public ProjectedAll transfer;
private ProgressDialog dialog;
public DataResponse dataR;

Intent myIntent; // gets the previously created intent
double x; // will return "x"
double y; // will return "y"
int spatial; // will return "spatial"

//public static Context appContext;

protected void onCreate(Bundle savedInstanceState) {

    StrictMode.ThreadPolicy policy = new StrictMode.

    dialog= new ProgressDialog(DisplayInfo.this);

        myIntent= getIntent(); // gets the previously created intent
         x = myIntent.getDoubleExtra("x",0); // will return "x"
         y = myIntent.getDoubleExtra("y", 0); // will return "y"
         spatial= myIntent.getIntExtra("spatial", 0); // will return "spatial"

        String URL = "Some URL"

dataR=new DataResponse().execute(attUp).get();

public void onStart()
{more code}


When you are using get, using Async Task doesn’t make any sense. Because get() will block the UI Thread, Thats why are facing 3 to 5 secs of blank screen as you have mentioned above.

Don’t use get() instead use AsyncTask with Call Back check this AsyncTask with callback interface

We are here to answer your question about Properly Using AsyncTask get() - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji