Android - AsyncTask and ProgressDialog

0

Hello, guys. I'm starting in the Mobile area recently and I'm having some issues with the user experience issue in my App.

In my App, it gets a JSON from the server through a AsyncTask . Below is a well-honed class, but it basically does:

  • onPreExecute : Starts ProgressDialog .
  • doInBackground : Search the JSON on the server using OkHttp .
  • onPostExecute : Fills a List with JSON content and ends ProgressDialog .

    class NetworkTask extends AsyncTask<Void, Void, String>{private ProgressDialog dialog;
    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Resources resource = getResources();
        String aguarde = resource.getString(R.string.aguarde);
        String sincronizando = resource.getString(R.string.sincronizando_dados);
        dialog = ProgressDialog.show(rootView.getContext(), aguarde,sincronizando, true);
    }
    
    @Override
    protected String doInBackground(Void... params) {
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(ServerUtil.URL_PROPRIETARIOS).build();
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            error += e.getMessage();
        }
        return null;
    }
    
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if(s != null){
            try {
                JSONArray jsonArray = new JSONArray(s);
                proprietarios = new Proprietarios(jsonArray);
                adapter = new ProprietarioAdapter(rootView.getContext(), proprietarios.getList(), gadoProprietario);
                lista.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
                error += e.getMessage();
            } catch(Exception e){
                e.printStackTrace();
            }
        }
        if(proprietarios != null && proprietarios.getList().size() > 0){
            for(Proprietario proprietario : proprietarios.getList()){
                new GadoProprietarioTask(proprietario.getId()).execute();
            }
        }
        dialog.dismiss();
    }
    

My question:

Eg: I try to connect and receive JSON in less than 100ms , there is no need to open ProgressDialog and close almost instantaneously. So I want to avoid this snapshot and I want it to only start after 200ms and if it gives 201ms it does not immediately close 200ms for example.

If the explanation is not good enough say that I redo, thank you in advance.

Note: AsyncTask is the best way for what I'm doing? If you know anything better, just go there!

    
asked by anonymous 01.06.2016 / 05:05

1 answer

1

At least you keep the UI occupied, the better. Honestly, I do not see this as a problem but I agree that it will aesthetically look strange.

In this case, you will need to remove ProgressDialog from class NetworkTask and pass it to the scope of which you are performing this task.

But that's not all, you'll also have to schedule a CountDownTimer or a Handler to trigger the task after a certain interval.

Example using Handler

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        new NetworkTask().execute();
    }
}, 200);

Example using Timer

new Timer().schedule(new TimerTask() {          
    @Override
    public void run() {
        new NetworkTask().execute()
    }
}, 200);

Of course, do not forget to display ProgressDialog before the timer you created so that it appears before the task is executed.

Also, do not forget to take into account that when we are testing networking, usually because the server is developmental, it may be faster to demand for n type factors, latency, server availability, data to be consulted, finally. you may not even have to make that change to your app release.

I hope you have helped.

    
01.06.2016 / 05:55