-3

My code will grab jsonString from web in AsyncTask, and then return this string (auth2) back to MainActivity.

I have problem that progressDialog wont show in AsyncTask.

My AsyncTask code:

    /**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MyActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected String doInBackground(String... params) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();
        //pass url string
        String url = params[0];
        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                JSONObject jsonUserInfo=jsonObj.getJSONObject("user_info");
                String auth2 = jsonUserInfo.getString("auth");
                return auth2;

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }
        return null;

    }

    @Override
    protected void onPostExecute(String auth2) {
        super.onPostExecute(auth2);
        // Dismiss the progress dialog
        if (pDialog.isShowing()){
            pDialog.dismiss();
        }

    }

}

What could be problem? Is it related to methods void,string etc?

user2033139
  • 573
  • 2
  • 10
  • 21

1 Answers1

-1

onProgressUpdate() is used to update progress on UI. It takes one parameter that corresponds to the second parameter in the AsyncTask class definition. This callback can be triggered from within the body of the doInBackground() method by calling publishProgress().

For more information, check this link and this link.

Community
  • 1
  • 1
Vamsi
  • 878
  • 1
  • 8
  • 25