ProgressDialog在onPreExecute()中没有显示



我看到。get()是问题,但我尝试没有他,什么也没有。如果可能的话,请帮助我。ProgressDialog在doInBackground()和onPostExecute "解散"之后执行,然后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(),并使用委托返回AsyncTask上的结果到UI线程,这里有一个例子

编辑:

你的界面应该是:

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

相关内容

  • 没有找到相关文章

最新更新