java - ProgressDialog not showing up in onPreExecute() -
i saw .get() problem, try without him , nothing. if possible me. progressdialog execute after doinbackground() , after run onpostexecute "dismiss" , progressdialog not show.
public list<usuario> getlistausuario(activity activity) { string[] aux = new string[3]; aux[0] = url_ws_usuario; string[] resposta = null; aux[2] = "get"; try { resposta = new webservicecliente(activity).execute(aux).get(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { // todo auto-generated catch block e.printstacktrace(); } if (resposta[0].equals("200")) { gson gson = new gson(); arraylist<usuario> listacliente = new arraylist<usuario>(); jsonparser parser = new jsonparser(); jsonarray array = parser.parse(resposta[1]).getasjsonarray(); (int = 0; < array.size(); i++) { listacliente.add(gson.fromjson(array.get(i), usuario.class)); } return listacliente; } else { return null; } } asynctask: public class webservicecliente extends asynctask<string, void, string[]> { private activity activity; private progressdialog pdialog; public webservicecliente(activity ac) { activity = ac; } public final string[] get(string url) { string[] result = new string[2]; httpget httpget = new httpget(url); httpresponse response; try { response = httpclientsingleton.gethttpclientinstace().execute( httpget); httpentity entity = response.getentity(); if (entity != null) { result[0] = string.valueof(response.getstatusline() .getstatuscode()); inputstream instream = entity.getcontent(); result[1] = tostring(instream); instream.close(); log.i("get", "result post jsonpost : " + result[0] + " : " + result[1]); } } catch (exception e) { log.e("ngvl", "falha ao acessar web service", e); result[0] = "0"; result[1] = "falha de rede!"; } return result; } public final string[] post(string url, string json) { string[] result = new string[2]; try { httppost httppost = new httppost(new uri(url)); httppost.setheader("content-type", "application/json"); stringentity sentity = new stringentity(json, "utf-8"); httppost.setentity(sentity); httpresponse response; response = httpclientsingleton.gethttpclientinstace().execute( httppost); httpentity entity = response.getentity(); if (entity != null) { result[0] = string.valueof(response.getstatusline() .getstatuscode()); inputstream instream = entity.getcontent(); result[1] = tostring(instream); instream.close(); log.d("post", "result post jsonpost : " + result[0] + " : " + result[1]); } } catch (exception e) { log.e("ngvl", "falha ao acessar web service", e); result[0] = "0"; result[1] = "falha de rede!"; } return result; } private string tostring(inputstream is) throws ioexception { byte[] bytes = new byte[1024]; bytearrayoutputstream baos = new bytearrayoutputstream(); int lidos; while ((lidos = is.read(bytes)) > 0) { baos.write(bytes, 0, lidos); } return new string(baos.tobytearray()); } @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(activity); pdialog.setcanceledontouchoutside(false); pdialog.setcancelable(false); pdialog.setindeterminate(true); pdialog.settitle("conectando servidor."); pdialog.setmessage("aguarde..."); pdialog.show(); } @override protected string[] doinbackground(string... params) { if (params[2] == "post") { return post(params[0], params[1]); } else if (params[2] == "get") { return get(params[0]); } else { return null; } } @override protected void onpostexecute(string[] params) { super.onpostexecute(params); try { // stop dialog if (pdialog.isshowing()) { pdialog.dismiss(); } } catch (exception e) { e.printstacktrace(); } } }
the problem
new webservicecliente(activity).execute(aux).get(); get() blocking call, , ui thread blocked waiting get() return there no 1 can take care of drawing progressdialog. remove get(), , use delegate return results on asynctask ui thread, here there example
edit:
your interface should like:
public interface callbackreciever { public void receivedata(string[] result); } the constructor of asyntask changes like
callbackreciever mlistener; public webservicecliente(activity ac, callbackreciever listener) { activity = ac; mlistener = listener; } in onpostexecute:
@override protected void onpostexecute(string[] params) { try { if (mlistener != null) { mlistener.receivedata(params); } // stop dialog if (pdialog.isshowing()) { pdialog.dismiss(); } } catch (exception e) { e.printstacktrace(); } } in receivedata, in activity, have process string[] result
Comments
Post a Comment