title: Passing string from doInBackground of Async task to main thread
body: <p>Semi-noob with Java here.</p>
  
  <p>I am trying to set a TextView inside my <code>doInBackground()</code> inside of my Async task.  According to my research, I can not modify the main thread doing this so messing around with TextViews is out of the question here.  So what I would like to do instead is use a string.  I need this string to be accessible in the main class. </p>
  
  <p>How can I do this?</p>
  
  <p>I tried <code>String loginresult = "Login Successful! Please Wait...";</code> but I am not able to access that string anywhere.  I tried marking it as <code>public</code> but that is an illegal modifier inside a doInBackground().</p>
  
  <p>Maybe strings is not the best way to go about doing this, if so, what would all you geniuses out there suggest?</p>
  
  <p>Here is my async code, I put arrows in the areas I am having an issue.  Any help would be appreciated for this noob :)</p>
  
  <pre><code>class PostTask extends AsyncTask&lt;String, Integer, String&gt; {
             @Override
             protected void onPreExecute() {
                super.onPreExecute();
  
  
  
             }
  
             @Override
             protected String doInBackground(String... params) {
  
  
                 try {
                      List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(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");
        -----&gt;                result.setText("Login Successful! Please Wait...");   
                      }else
                      {
                          Log.w("SENCIDE", "FALSE");
      ------&gt;             result.setText(str);                
                      }
  
                  } catch (ClientProtocolException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
  
  
                // Dummy code
                for (int i = 0; i &lt;= 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 + "&amp;pword=" + password2);
                        startActivity(intent);
                     }
  
             }//end onPostExecute
             }//end async task
  </code></pre>