Android从onPostExecute()显示ProgressDialog

时间:2016-09-13 18:26:34

标签: android progressdialog

我有一个AsyncTask,在ProgressDialog显示onPreExecute()时,它运行正常。

但是在我的应用中,我会首先查看AsyncTask,如果有任何更新待处理以适用于该应用,只有在是的时候,我会显示ProgressDialog

这发生在onPostExecute()。但是,当尝试从ProgressDialog显示onPostExecute()时,它永远不会出现在屏幕上。当onPreExecute()onPostExecute()在UI上运行时,应该显示它。

有什么建议吗?

class ws_update extends AsyncTask<Integer, Integer, Integer>
{
    ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(NoticiasView.this);
        pd.setMessage(getResources().getString(R.string.ws_update));
        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setIndeterminate(false);
        pd.setCanceledOnTouchOutside(false);
    }

    @Override
    protected Integer doInBackground(Integer... params)
    {
        HttpURLConnection connection = null;
        try {
            String strURLact = Delegate.url_WS + "wsposts_v1.php";
            URL url = new URL(strURLact);

            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");
            connection.setUseCaches(true);
            connection.setRequestProperty("content-type", "application/json;  charset=utf-8");

            DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());

            ...


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(connection != null)
                connection.disconnect();
        }

        return null;

    }

    @Override
    protected void onProgressUpdate(Integer... values)
    {
        super.onProgressUpdate(values);
        pd.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);

        if (jsonResponse == null) {
            return;
        }

        try {
            JSONArray aWS = jsonResponse.getJSONArray("wsposts");
            if (aWS.length() <= 0) {
                return;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        pd.show();

        try {
            db.open();

            JSONArray aWS = jsonResponse.getJSONArray("wsposts");
            for (int iLoop=0; iLoop<aWS.length(); iLoop++) {
                JSONObject dicWS = aWS.getJSONObject(iLoop);
                JSONObject dicPost = dicWS.getJSONObject("post");
           ...

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        pd.dismiss();
    }
}

0 个答案:

没有答案