如何为AsyncTask线程集成进度条

时间:2014-12-20 19:48:15

标签: android multithreading android-asynctask

我正在开发一个应用程序,它从用户获取数据并将其发布到Web服务。网络操作发生在AsyncTask线程中,有时Web服务需要大约3秒才能获取,处理和发送回复。我想要一个显示活动进度的进度条。我搜索了一下,发现了一些指南,但其中一些是关于视频处理等等,我无法理解如何根据我的需要进行整合。

我发布了我的AsyncTask线程代码,请帮我集成进度条。

 private class DownloadOperation extends AsyncTask<Void, Void, String> {
        String uname = "";
        String email = "";
        String password = "";
        String confirmpass = "";
        String phone = "";

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Get user defined values
            uname = username.getText().toString();
            email = mail.getText().toString();
            password = pass.getText().toString();
            confirmpass = cpass.getText().toString();
            phone = phn.getText().toString();

            //Progress Bar should go here?

        }

        @Override
        protected String doInBackground(Void... params) {
            String response = "";
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://rgbpallete.in/led/api/signup");
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
                nameValuePairs.add(new BasicNameValuePair("uname", uname));
                nameValuePairs.add(new BasicNameValuePair("pass", password));
                nameValuePairs.add(new BasicNameValuePair("email", email));
                nameValuePairs.add(new BasicNameValuePair("phone", phone));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpResponse = httpclient.execute(httppost);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                httpEntity = httpResponse.getEntity();
                response = EntityUtils.toString(httpEntity);
                return response;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String jsonStr) {
            super.onPostExecute(jsonStr);
            Log.d("tag", "Result:\n" + jsonStr);
            if (jsonStr != null) {
                try{
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    String message = jsonObj.getString("message");
                    boolean error = jsonObj.getBoolean("error");

                    error(error,message);

                }
                catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
        }
    }

这是布局signup.xml

中进度条的.xml代码
<ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_below="@+id/comfirmpass"
        android:layout_centerHorizontal="true"
        android:indeterminate="false" />

1 个答案:

答案 0 :(得分:2)

<强> 1。不确定的进展:......

对于不确定的进度条,这足以

  1. onPreExecute方法中显示,
  2. 将其隐藏在onPostExecute扩展程序的AsyncTask方法中。
  3. <强> 2。确定进度:

    要将确定的进度条绑定到后台进程,您需要从doInBackground方法调用publishProgress。这反过来将通过onProgressUpdate通知UI线程,所以如果你也覆盖那个线程,你将能够显示正确的进度。

    为此,您还需要从HttpClient方式切换到UrlConnection方式,并“手动”处理其输出。