-2

Unable to start Progress Dialog (Not visible) while creating a reusable asyncTask class.

Parent class code(where calling asynctask) :

            receivedData = new JSONParser(this).execute(urlstring).get();

AsyncTask class code:

              package com.nearbybazar.vendor2;

            import android.app.ProgressDialog;
            import android.content.Context;
            import android.os.AsyncTask;

            import java.io.BufferedReader;
            import java.io.InputStream;
            import java.io.InputStreamReader;
            import java.net.HttpURLConnection;
            import java.net.URL;
            import java.util.HashMap;

            import static android.app.ProgressDialog.STYLE_SPINNER;

            public class JSONParser extends AsyncTask<String, Void, String> {

                private ProgressDialog dialog;
            public JSONParser(Context con) {
                    //Context parentContext = this.getApplicationContext();
                    dialog = new ProgressDialog(con);
                dialog.setProgressStyle(STYLE_SPINNER);

                }

                @Override
                protected void onPreExecute() {

                    dialog.setMessage("Loading");
                    dialog.show();
                }

                protected String doInBackground(String... params) {
                    try {
                        String url=params[0];
                        URL Url = new URL(url);
                        HttpURLConnection connection = (HttpURLConnection) Url.openConnection();
                        InputStream is = connection.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        StringBuilder sb = new StringBuilder();
                        String line;
                        System.out.println(url);
                        while ((line = br.readLine()) != null) {
                            sb.append(line);
                        }
                        line = sb.toString();
                        connection.disconnect();
                        is.close();
                        sb.delete(0, sb.length());
                        return line;
                    } catch (Exception e) {
                        e.printStackTrace();
                        return null;
                    }
                   // return line;
                }

                String url = null;

               /* public JSONParser(String url) {
               /* this.execute(url);
                    this.url = url;
                }*/
            @Override
            protected void onPostExecute(String result)
            {
                if( null != dialog)
                    dialog.dismiss();
            }

            }

although the async class working fine and giving correct results but progressdialog is not visible. asynctask is also taking some time to load, for that loading time my phone's notification bar turns white.

Praful Sharma
  • 21
  • 1
  • 8

4 Answers4

0

Remove initialisation from constructor and move inside onPreExecute and create a global Context and inside the constructor initialise. For example:

   private ProgressDialog dialog;
   private Context con;
        public JSONParser(Context con) {
                //Context parentContext = this.getApplicationContext();
             this.con = con;

            }

            @Override
            protected void onPreExecute() {
            dialog = new ProgressDialog(con);
            dialog.setProgressStyle(STYLE_SPINNER);
                dialog.setMessage("Loading");
                dialog.show();
            }

And execution of AsyncTask should be without get() method.

Yupi
  • 4,402
  • 3
  • 18
  • 37
0

You should not block the main UI thread waiting for the AsyncTask to terminated, if the UI is blocked nothing is drawn/shown and your app becomes irresponsible. The place to process the doInBackground() returned value is onPostExecute()

from56
  • 3,976
  • 2
  • 13
  • 23
0

Try calling the your AsyncTask like this:

JSONParser task = new JSONParser(this);
task.execute(urlstring);

instead of:

receivedData = new JSONParser(this).execute(urlstring).get();

According to Android Docs:

Result get ()

Waits if necessary for the computation to complete, and then retrieves its result.

https://developer.android.com/reference/android/os/AsyncTask.html#get()

.

Update:

In order to get the data from your AsyncTask create a new Java file and call it something like "OnJsonParserListener". Inside the new file put something like this:

public interface OnJsonParserListener{
    void onCompletedJsonParse(String result);
}

Now just implement the interface and in the onPostExecute method pass the result back through onCompletedJsonParse(result).

Barns
  • 4,850
  • 3
  • 17
  • 31
  • then how to get the receiveddata? – Praful Sharma Jan 30 '18 at 22:08
  • @PrafulSharma :: You need to create a public `Interface`. Pass the result through it. – Barns Jan 30 '18 at 23:01
  • @PrafulSharma :: If you need more help on how to implement an `interface` take a look at this post :: https://stackoverflow.com/questions/994840/how-to-create-our-own-listener-interface-in-android :: It is really quite well explained. – Barns Jan 30 '18 at 23:13
  • You don't need an interface, just save it in a class variable or call the method that needs the data – Juan Cruz Soler Jan 30 '18 at 23:35
-1

Try passing activity into the constructor of asynck task and invoke progressdialg from activity instead of context. This should solve your issue.