-1

I know this is duplicate but trust me i have tried many of the available codes and still not getting this worked.

Below are my code

public class LookIn extends AppCompatActivity implements View.OnClickListener{
 @Override
    protected void onCreate(Bundle savedInstanceState) {
    //Initializing properties
}
public void onClick(View v) { 
// Event for button click
callHelper();
}
private void callHelper()
{
    List result=null;
    try {
                 String jsonResponse=new CallmeGetHelperAsyncTask(this).execute(params).get();
                result= RestUtil.getUserList(jsonResponse);
    }
    catch (InterruptedException e) {
                Log.e("Callme", Log.getStackTraceString(e));
    } catch (ExecutionException e) {
                Log.e("Callme", Log.getStackTraceString(e));
        }
        catch(JSONException x)
        {
            Log.e("Callme",Log.getStackTraceString(x) );
        }
    }
}

Below is my AsyncTask class

    public class CallmeGetHelperAsyncTask extends AsyncTask<String,Void,String > {
private AppCompatActivity activity=null;
    private ProgressDialog dialog=null;
    public CallmeGetHelperAsyncTask(){}
    public CallmeGetHelperAsyncTask(AppCompatActivity activity){
        this.activity=activity;
        //this.dialog= dialog;
    }

    @Override
    protected void onPreExecute() {
    if(this.dialog!=null)
        {
            Log.v("Callme", "Starting Dialog");

            dialog = ProgressDialog.show(this.activity, "title", "message");
          //  this.dialog.setMessage("Looking for Helpers");
            this.dialog.show();
        }
        else
        {
            dialog = ProgressDialog.show(this.activity, "title", "message");

        }
    }
    @Override
    protected void onPostExecute(String s) {
        if(this.dialog!=null)
        {
           Log.v("Callme","Closing Dialog");
           this.dialog.dismiss();
        }
        else
        {
            Log.v("Callme","Dialog is not initiated in CallmeGethelperAsyncTask");
        }
    }
    @Override
    protected String doInBackground(String... params) {
    //call to webservice
     }
    }

I am not able to get what is the problem with above code.

One more bizarre i found(bizarre may be for me only because i am new). I tried printing Thread.currentThread.getId() from both my ActivityClass as well as from my AsyncTask and surprisingly both printed "1".

If i am not wrong then that says both codes are running from the same thread.

About this i have read Running multiple AsyncTasks at the same time -- not possible? which says earlier threadpool contained only 1 thread but i am using Android 5.1 which is supposed to contain 5 threads. So again i am confused over it.

Kindly take some time to explain. Thank you

Community
  • 1
  • 1
Kumar Gaurav
  • 1,287
  • 3
  • 19
  • 47

1 Answers1

0

Replace this:

public CallmeGetHelperAsyncTask(AppCompatActivity activity){
        this.activity=activity;
        //this.dialog= dialog;
    }

with:

private Context context;
public CallmeGetHelperAsyncTask(Context context){
        this.context=context;
    }

and this:

dialog = ProgressDialog.show(this.activity, "title", "message");

with:

dialog = ProgressDialog.show(this.context, "title", "message");

and this:

String jsonResponse=new CallmeGetHelperAsyncTask(this).execute(params).get();

with:

String jsonResponse=new CallmeGetHelperAsyncTask(LookIn.this).execute(params).get();

Although i would rather not use the static method show of the progress dialog and build an Actual Progress Dialog.

Hope it helps!!!!

Kostas Drak
  • 3,222
  • 6
  • 28
  • 60