gpt4 book ai didi

java - ProgressDialog 未显示在 onPreExecute() 中

转载 作者:行者123 更新时间:2023-11-30 08:51:26 26 4
gpt4 key购买 nike

我看到 .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();
}

}

}

最佳答案

问题是

new WebServiceCliente(activity).execute(aux).get();

get() 是一个阻塞调用,UI Thread 阻塞等待 get() 返回,没有人可以负责绘制 ProgressDialog。删除 get(),并使用委托(delegate)将 AsyncTask 上的结果返回到 UI 线程,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

关于java - ProgressDialog 未显示在 onPreExecute() 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30549595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com