title: Print JSON response on Android Screen
body: <p>I have my Android emulator for the time being connecting to my local computer. It contacts the PHP and posts an IP address to be run. It then echoes a JSON result. I want to print the result, but am having trouble getting the android screen to change.</p>
  
  <p>Here's my activity:</p>
  
  <pre><code>public class MainActivity extends Activity implements OnClickListener {
  
  EditText ipAddress;
  Button bSearch;
  
  String IP;
  
  HttpClient httpclient;
  
  HttpPost httppost;
  
  ArrayList&lt;NameValuePair&gt; nameValuePairs;
  
  HttpResponse response;
  HttpEntity entity;
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      initialize();
      }
  
  private void initialize(){
      ipAddress = (EditText) findViewById(R.id.IPaddress);
      bSearch = (Button) findViewById(R.id.searchBtn);
  
      bSearch.setOnClickListener((OnClickListener) this);
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
  }
  
  @Override
  public void onClick(View arg0) {
      // TODO Auto-generated method stub
  
      httpclient = new DefaultHttpClient();
  
      httppost = new HttpPost("http://127.0.0.1/myfiles/WorkingVersionVJSON.php");
  
      IP = ipAddress.getText().toString();
  
      nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
  
      nameValuePairs.add(new BasicNameValuePair("ipaddress", IP));
  
      try{
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
          response = httpclient.execute(httppost);
  
          if(response.getStatusLine().getStatusCode()==200){
  
              entity = response.getEntity();
  
              if(entity != null){
  
                  InputStream instream = entity.getContent();
  
                  JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
  
                  String rIP = jsonResponse.getString("ipaddress");
  
              }
          }
      }catch(Exception e){
          e.printStackTrace();
      }
  }
  
   private static String convertStreamToString(InputStream is) {
          /*
           * To convert the InputStream to String we use the BufferedReader.readLine()
           * method. We iterate until the BufferedReader return null which means
           * there's no more data to read. Each line will appended to a StringBuilder
           * and returned as String.
           */
          BufferedReader reader = new BufferedReader(new InputStreamReader(is));
          StringBuilder sb = new StringBuilder();
  
          String line = null;
          try {
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              try {
                  is.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return sb.toString();
      }
  }
  </code></pre>
  
  <p>And the PHP</p>
  
  <pre><code>&lt;?php
  
  
  
   require("IPQFunctionworkingversionV7.php");
  $ipaddress = $_POST["ipaddress"];
  
  $results = array();
  
  $results = getScore($ipaddress);
  echo json_encode($results);
  ?&gt;
  </code></pre>
  
  <p>The PHP works when I run it in my browser and post to it using html. I assume that the line</p>
  
  <pre><code>nameValuePairs.add(new BasicNameValuePair("ipaddress", IP));
  </code></pre>
  
  <p>Posts to the php the same way, since IP should be what's entered into the textbox on the android and it matches the POST in the PHP. </p>
  
  <p>The activity compiles and is on the emulator, so I assume the connection is successful. I just need to parse the JSON response in a way that I can get it to display on the screen. Once I have that I'll work on the layout. </p>