How to convert the output of a PHP into parsed data through JSON

I have the following php file working fine on a webhost.

<?php
   $dbhost = 'dbhost';
   $dbuser = 'dbuser';
   $dbpass = 'pass';

   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn )
   {
      die('Could not connect'.mysql_error());
   }

   $sql = "SELECT FIELD1, FIELD2, FIELD3, FIELD4 FROM `TABLE` WHERE FIELD1 =     'VALUE1' ";
   mysql_select_db('MyDB');
   $retval = mysql_query( $sql, $conn );

   if(! $retval )
   {
      die('Could not get data: ' . mysql_error());
   }

   while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
   {
      echo "FIELD1" :{$row['FIELD1']}  <br> ".
         "FIELD2 : {$row['FIELD2']} <br> ".
         "FIELD3 : {$row['FIELD3']} <br> ";
      $output[] = $row;
   }

   print(json_encode($row));
   mysql_close($conn);
?>

Link is here http://marcodr.byethost7.com/TP2.php

Now, I would like to show this content in a TextView and I am unseccessfully using the following code

JSONObject object=getJSONObject(“result”); //comes from the     print(json_encode ($result));
JSONArray arr=object.getJSONArray(“Value1”); //transforms the object Value1 in array
for(int i=0;i<arr.length;i++){
JSONObject obj1=arr.getJSONObject(i);
String Value1=obj1.getString("Value1");
TextView.setText(Value1); //set TextView’s content to the String Value1
}

The manifest has permission to navigate the internet, the text view is in the layout etc, but STILL I can’t put the PHP output in the TextView. What is wrong in my code?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s