将字符串从Async任务的doInBackground传递到主线程

时间:2014-01-06 05:52:34

标签: java android asynchronous android-asynctask

这里有Java的半菜鸟。

我正在尝试在我的异步任务中的doInBackground()内部设置TextView。根据我的研究,我无法修改主线程这样做,所以搞乱TextViews是不可能的。所以我想做的是使用字符串。我需要在主类中访问此字符串。

我该怎么做?

我尝试了String loginresult = "Login Successful! Please Wait...";,但我无法在任何地方访问该字符串。我尝试将其标记为public,但这是doInBackground()中的非法修饰符。

也许字符串不是最好的方法,如果是这样,那么你们所有的天才会有什么建议呢?

这是我的异步代码,我把箭头放在我遇到问题的区域。任何帮助将不胜感激这个菜鸟:)

class PostTask extends AsyncTask<String, Integer, String> {
           @Override
           protected void onPreExecute() {
              super.onPreExecute();



           }

           @Override
           protected String doInBackground(String... params) {


               try {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                    nameValuePairs.add(new BasicNameValuePair("username", username));
                    nameValuePairs.add(new BasicNameValuePair("password", password));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                    Log.w("SENCIDE", "Execute HTTP Post Request");
                    //Executes link, login.php returns true if username and password match in db 
                    HttpResponse response = httpclient.execute(httppost);

                   String str = inputStreamToString(response.getEntity().getContent()).toString();
                    Log.w("SENCIDE", str);

                    if(str.toString().equalsIgnoreCase("true"))
                    {
                        Log.w("SENCIDE", "TRUE");
      ----->                result.setText("Login Successful! Please Wait...");   
                    }else
                    {
                        Log.w("SENCIDE", "FALSE");
    ------>             result.setText(str);                
                    }

                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }



              // Dummy code
              for (int i = 0; i <= 100; i += 5) {
                try {
                  Thread.sleep(50);
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
                 publishProgress(i);
              }
              return "All Done!";
           }//end doinbackground

            StringBuilder inputStreamToString(InputStream is) {
                String line = "";
                StringBuilder total = new StringBuilder();
                // Wrap a BufferedReader around the InputStream
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                // Read response until the end
                try {
                    while ((line = rd.readLine()) != null) { 
                        total.append(line); 
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // Return full string
                return total;
            }//end StringBuilder

           @Override
           protected void onProgressUpdate(Integer... values) {
              super.onProgressUpdate(values);

           }

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

                // turns the text in the textview "Tbl_result" into a text string called "tblresult"
                TextView tblresult = (TextView) findViewById(R.id.tbl_result);




        // If "tblresult" text string matches the string "Login Successful! Please Wait..." exactly, it will switch to next activity
                if (tblresult.getText().toString().equals("Login Successful! Please Wait...")) {
                      Intent intent = new Intent(NewAndroidLogin.this, Homepage.class);
                     //take text in the username/password text boxes and put them into an extra and push to next activity 
                      EditText uname2 = (EditText)findViewById(R.id.txt_username);
                      String username2 = uname2.getText().toString();
                      EditText pword2 = (EditText)findViewById(R.id.txt_password);
                      String password2 = pword2.getText().toString();
                      intent.putExtra("username2", username2 + "&pword=" + password2);
                      startActivity(intent);
                   }

           }//end onPostExecute
           }//end async task

3 个答案:

答案 0 :(得分:0)

String loginresult = "Login Successful! Please Wait...";设为全局

runOnUiThread(new Runnable() {
@Override
public void run() {
str = inputStreamToString(response.getEntity().getContent()).toString();
if(str.toString().equalsIgnoreCase("true"))
{
 Log.w("SENCIDE", "TRUE");
 result.setText("Login Successful! Please Wait...");   
}
else
{
 Log.w("SENCIDE", "FALSE");
 result.setText(str);                
}
}
} );

答案 1 :(得分:0)

将您的AsyncTask更改为使用String作为进度参数类型:

AsyncTask<String, String, String>

更改onProgressUpdate()以更新进度

@Override
protected void onProgressUpdate(String... values) {
    result.setText(values[0]);
}

然后报告进展情况:

if(str.toString().equalsIgnoreCase("true"))
{
    Log.w("SENCIDE", "TRUE");
    publishProgress("Login Successful! Please Wait...");   
}else
{
    Log.w("SENCIDE", "FALSE");
    publishProgress(str);                
}

答案 2 :(得分:0)

在班级宣布处理程序:

 Handler handler;

在Activity的onCreate()方法中初始化Handler:

// Doing this inside the onCreate() method of Activity
// will help the handler to hold the reference to this Activity.

 handler = new Handler();

在后台线程中调用它:

 @Override
 protected String doInBackground(String... params) {

    handler.post(new Runnable(){

    public void run(){

       // SET UI COMPONENTS FROM HERE.
    }

   });
 }