title: Taking data parsed with JSON and displaying it as text in Android
body: <p>I am using JSON to access my mySQL database and then return product information such as the UPCA, Product, and Company. I am able to get the information to display in grey text but not appear in text view. How can I get it to appear in text view. I have reviewed other questions but I cannot find one that would specifically help me. Any information on how to do this would be great. Maybe I am missing something? Thank you in advance</p>
  
  <p>This is my Main file package </p>
  
  <pre><code>net.example.glutefree;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.content.Intent;
  import android.widget.Button;
  import android.widget.EditText;
  //import android.widget.TextView;
  import android.view.View;
  import android.view.View.OnClickListener;
  
  //Links GLUTEFREE with NEWACTIVITY
  public class GluteFree extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.glute_free);
          mMyEditText = (EditText) findViewById(R.id.editText1);
          Button pressToScan = (Button) findViewById(R.id.button1);
  
          pressToScan.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  Intent data = new Intent("com.google.zxing.client.android.SCAN");
                  data.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE",
                          "QR_CODE_MODE");
                  setResult(RESULT_OK, data);
                  startActivityForResult(data, 0);
              }
          });
      }
  
  
  
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
           String UPC = null;
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == 0) {
              if (resultCode == RESULT_OK) {
                  UPC = data.getStringExtra("SCAN_RESULT");
                  // moved here
                  Intent i = new Intent("net.example.glutefree.Networking");
                  i.putExtra("UPCA", UPC);
                  startActivity(i);
                  EditText tv = (EditText) findViewById(R.id.editText1);
                  tv.setText(UPC);
                  // Handle successful scan
              } else if (resultCode == RESULT_CANCELED) {
                  // Handle cancel
              }
  
          }
  
      }
  
      // Click this Link to open Links Page
      public void onClickLinks(View view) {
          ;
      }
      //Click this Link to open the Previous Result
      public void onClickResult(View view) {
          startActivity(new Intent("net.example.glutefree.Networking"));
      }
      //Manually Search the UPC that is in the box
      //FIND CODE TO MAKE THAT WORK
      private EditText mMyEditText;
      public void onClickSearch(View view) { 
  
          String UPC = mMyEditText.getText().toString();
          Intent intent = new Intent(this, Networking.class);   
          intent.putExtra("UPCA", UPC);  //text is some key used to retrieve value in NextActivity 
          startActivity(intent);
      }
  }
  </code></pre>
  
  <p>This is my Database connection file.</p>
  
  <pre><code>package net.example.glutefree;
  
  import android.app.Activity;
  import android.os.Bundle;
  import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  import android.util.Log;
  import android.widget.TextView;
  
  
  
  
  
  public class Networking extends Activity{
      TextView txt;
      int request_Code = 1;
  //called when activity is first created
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_networking);
      txt = new TextView(getApplicationContext());
      // Set the text and call the connect function. 
      txt.setText("Connecting...");
    //call the method to run the data retrieval
      new Thread() {
          public void run() {
              final String data = getServerData(KEY_121);
              if (data != null)
                  runOnUiThread(new Runnable()
                  {
                      public void run()
                      {
                         txt.setText(data);
  
                      }
                  });
          }
      }.start();
  }
  
  
  
  public static final String KEY_121 = "http://website.com/scripts/application_query.php";
  
  private String getServerData(String returnString) {
     String UPC = getIntent().getStringExtra("UPCA");
     InputStream is = null;
     String result = "";
      //the upc data to send
      ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
  
      nameValuePairs.add(new BasicNameValuePair("UPCA",UPC));
  
      //http post
      try{
  
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(KEY_121);
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity entity = response.getEntity();
              is = entity.getContent();
  
      }catch(Exception e){
              Log.e("log_tag", "Error in http connection "+e.toString());
      }
  
      //convert response to string
      try{
              BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
              }
              is.close();
              result=sb.toString();
      }catch(Exception e){
              Log.e("log_tag", "Error converting result "+e.toString());
      }
      //parse json data
      try{
              JSONArray jArray = new JSONArray(result);
              Log.e("log_tag", "Result ");
              for(int i=0;i&lt;jArray.length();i++){
                      JSONObject json_data = jArray.getJSONObject(i);
                      String UPCA = json_data.getString("UPCA");
                      String Product = json_data.getString("Product");
                      String Glute = json_data.getString("Gluten Free");
                      returnString += "\n\t" + jArray.getJSONObject(i);
              }
      }catch(JSONException e){
              Log.e("log_tag", "Error parsing data "+e.toString());
      }
      return returnString;
  }
  
  }
  </code></pre>