【问题标题】:Asynctask with HttpClient and Progressbar带有 HttpClient 和 Progressbar 的异步任务
【发布时间】:2011-10-16 10:54:34
【问题描述】:

我正在尝试让我的 HttpClient 与包括进度条的 Asynctask 一起使用。 我已经有了这个 Asynctask 的代码:

private class httpClient extends AsyncTask<String, Boolean, String> {

private HttpClient httpClient = getHttpClient();

@Override
protected String doInBackground(String... params) {
    publishProgress(true); 
    // Do the usual httpclient thing to get the result
    return null;
} 

@Override 
protected void onProgressUpdate(Boolean... progress) { 
    // line below coupled with  
    //    getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS)  
    //    before setContentView  
    // will show the wait animation on the top-right corner 
    AndroidLogin.this.setProgressBarIndeterminateVisibility(progress[0]); 
} 

@Override 
protected void onPostExecute(String result) { 
    publishProgress(false); 
    // Do something with result in your activity 
} 

}

我的问题是如何在 Asynctask 中正确放置以下代码,并且 Progressbar 也可以正常工作。 有人可以帮忙吗?

/******* INPUT USERNAME AND PASSWORD *******/
        EditText uname = (EditText)findViewById(R.id.txt_username); // user input through EditText
        String username = uname.getText().toString();
        EditText pword = (EditText)findViewById(R.id.txt_password); // password input through EditText
        String password = pword.getText().toString();

/******* INSTANTIATE THE CUSTOM HTTPCLIENT *******/
        HttpClient httpClient = getHttpClient();
        HttpPost request = new HttpPost("https://xxx"); 

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
        nameValuePairs.add(new BasicNameValuePair("usr_name", username)); 
        nameValuePairs.add(new BasicNameValuePair("usr_password", password)); 
        request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

/******* EXECUTE HTTPCLIENT *******/
        try  
        { 
        HttpResponse response = httpClient.execute(request); 

/******* READ CONTENT IN BUFFER *******/
        InputStream inputStreamActivity = response.getEntity().getContent();  
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStreamActivity));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
        }
/******* CLOSE CONNECTION AND STREAM *******/
        System.out.println(sb);
            inputStreamActivity.close();
        httpClient.getConnectionManager().shutdown();

【问题讨论】:

    标签: android progress-bar httpclient android-asynctask


    【解决方案1】:

    我得到了解决方案。 我需要将文本字段包含到异步任务中,以避免异常。

    EditText uname = (EditText)findViewById(R.id.txt_username);
    EditText pword = (EditText)findViewById(R.id.txt_password);
    

    我决定使用 ProgressDialog 而不是 Progressbar,需要添加以下内容:

    //Create a Progressdialog
    ProgressDialog pd = new ProgressDialog(MyApp.this);
    
    @Override 
        protected void onPreExecute()  
        { 
            //Set the Progressdialog
            pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
            pd.setMessage("Getting data....");
                     }
    
     @Override
        protected void onProgressUpdate(Integer... args) { 
               //Update the Progressdialog
               pd.setProgress(args[0]); 
               } 
    
      @Override 
        protected void onPostExecute(String result) { 
            //Dismiss Progressdialog
            pd.dismiss();
                     }
    

    就是这样..

    【讨论】:

      【解决方案2】:

      尝试将 onPreExecute 用于 ProgressBar init 以及其他与您无关的重要内容:

      @Override
      protected void onPreExecute() 
      {
           publishProgress(true); 
      }
      
      @Override
      protected String doInBackground(String... params)
      {
           funtiontoallthatbigcodeorcodeitselfhere(); 
      }
      
      ...
      

      【讨论】:

      • 嗨 A.Quiroga,我现在用力关闭,似乎不起作用。
      • 10-16 13:32:52.735: WARN/dalvikvm(8257): threadid=1: thread exiting with unaught exception (group=0x401ba560)
      • 我也不得不尝试一下:nameValuePairs.add(new BasicNameValuePair("usr_password", password));
      • 这里是来自 Logcat 的错误:ERROR/AndroidRuntime(8675): java.lang.RuntimeException: Unable to instance activity ComponentInfo{com.sencide/com.sencide.AndroidLogin}: java.lang.NullPointerException
      • A.Quiroga,你还在吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多