简体   繁体   中英

Showing progressdialog for asynchronous httprequest

Requirement

Show the 2nd fragment to the user only when HttpRequest is complete and valid.

As of now

By referring various post from SO, it is learnt that we need to use AsyncTask to send HttpRequest and we've placed it in a separate class file with name Httpclientrequest.java

public class Httpclientrequest extends AsyncTask<String,Void,String> {
    protected ProgressDialog progressDialog;
    private Context mContext;
    public Httpclientrequest(Context context){
        mContext=context;
    }

    @Override
    protected void onPreExecute(){
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage("Processing...");
        progressDialog.setTitle("Sending request");
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
       //perform httprequest
       //return String
    }

    @Override
    protected void onPostExecute(String result)
    {
        progressDialog.dismiss();
    }
}

and in my Fragment1.java on clickeventlistener , I've below things done.

public void onClick(View view) {
      Httpclientrequest httpClientRequest = new Httpclientrequest(getContext());
      try {
             Object result = httpClientRequest.execute().get();
             FragmentTransaction ft= getFragmentManager().beginTransaction();
             ft.replace(R.id.content,new Fragment2());
             ft.commit();
      } catch (InterruptedException e) {
             e.printStackTrace();
      } catch (ExecutionException e) {
             e.printStackTrace();
      }
}

With the above way, the progressdialog will never show until httprequest is complete and appears and disappears for fraction of second before next fragment is loaded. When searched for this behavior , we've found that .get() on execute() blocks the Main UI thread. Now when get() is removed, it shows progressdialog before request , but by then 2nd fragment is loaded, without waiting for request to complete. It is the requirement that based on some condition from response, 2nd fragment has to be shown.

Question - How should we actually await for the AsyncTask along with ProgressDialog shown in the screen? Are we going out of track here, which can be done in more simpler way?

Put your fragment transaction code in onPostExecute in Httpclientrequest like this using .execute();

@Override
    protected void onPostExecute(String result)
    {
        progressDialog.dismiss();
        FragmentTransaction ft= context.getFragmentManager().beginTransaction();
        ft.replace(R.id.content,new Fragment2());
        ft.commit();
    }

if you are not able to find getFragmentManager() than you have to pass getActivity() in Httpclientrequest constructor and cast context like this

FragmentTransaction ft=((Activity)context).getFragmentManager().beginTransaction();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM