我看到.get()就是问题所在,但我没有他也没有尝试。如有可能帮助我。 ProgressDialog在doInBackground()之后执行,之后运行onPostExecute" dismiss"然后没有显示ProgressDialog。
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();
for (int i = 0; i < array.size(); i++) {
listaCliente.add(gson.fromJson(array.get(i), Usuario.class));
}
return listaCliente;
} else {
return null;
}
}
MY 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 from 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 from 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();
}
}
}
答案 0 :(得分:2)
问题是
new WebServiceCliente(activity).execute(aux).get();
get()
是一个阻止调用,如果UI Thread
被阻止等待get()
返回,则没有人可以负责绘制ProgressDialog
。删除get()
,并使用委托将AsyncTask
上的结果返回给用户界限主题,Here有一个示例
编辑:
你的界面应该是:
public interface CallbackReciever { public void receiveData(String[] result); }
你的AsynTask的构造函数改变了
CallbackReciever mListener;
public WebServiceCliente(Activity ac, CallbackReciever listener) {
activity = ac;
mListener = listener;
}
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();
}
}
在receiveData
Activity
中,您需要处理String[] result