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
: StartsProgressDialog
. -
doInBackground
: Search the JSON on the server usingOkHttp
. -
onPostExecute
: Fills aList
with JSON content and endsProgressDialog
.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!