简体   繁体   English

使用HttpClient和Progressbar进行Asynctask

[英]Asynctask with HttpClient and Progressbar

I am trying to get my HttpClient to work with Asynctask including a progressbar. 我试图让我的HttpClient与Asynctask一起工作,包括一个进度条。 I already have this code of the 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 
} 

}

My question is how to put below code correctly in the Asynctask, and that the Progressbar is also working. 我的问题是如何在Asynctask中正确放置代码,并且Progressbar也正常工作。 Can somebody please help? 有人可以帮忙吗?

/******* 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();

I got the solution. 我得到了解决方案。 I needed to include the Textfields into the Asynctask, to avoid the Exception. 我需要将Textfields包含在Asynctask中,以避免异常。

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

I decided to use a ProgressDialog instead of a Progressbar, following needs to be added: 我决定使用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();
                 }

That`s it.. 那就是..

Try to use onPreExecute for ProgressBar init and among another things which are not about your important stuff: 尝试使用onPreExecute for ProgressBar init以及其他不重要的东西:

@Override
protected void onPreExecute() 
{
     publishProgress(true); 
}

@Override
protected String doInBackground(String... params)
{
     funtiontoallthatbigcodeorcodeitselfhere(); 
}

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM