0
Hey there, guys. I’m just starting in the Mobile area and I’m having some problems with the issue of user experience in my App.
In my App, it obtains a JSON from the server through a AsyncTask
.
Below a class well fulfilled, but that basically makes:
onPreExecute
: Initiates aProgressDialog
.doInBackground
: Search the JSON on the server usingOkHttp
.onPostExecute
: Fills aList
with the content of JSON and ends theProgressDialog
.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 doubt:
Ex.: I try to connect and receive JSON in less than 100ms, there is no need to open a ProgressDialog
and be closed almost instantly. So I want to avoid this instant open and close feat I want it to start only after 200ms and if so 201ms it does not close immediately get over 200ms for example.
If the explanation is not good enough say that I redo, I thank you.
Obs.: AsyncTask
is the best way for what I’m doing? If you know any better comment there!
Thanks, I had already found here some examples with Countdowntimer that meet my need, but you gave me the answer I was needing. There really is the issue of latency and N extra factors on the server and maybe not even necessary outside the development environment, but really I was bothered by the UX of Dialog kkkk. Thanks!
– Victor Magalhães