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?
In PHP side:
$obj = new stdClass();
$obj->label="Price of an item";
$obj->data = array(
array('1999','200'),
array('2000','210'),
array('2007','240')
);
echo json_encode($obj);
Let's suppose your JSON is this:
[
{
"response": [
{
"1999": "200",
"2000": "210",
"2007": "240"
}
]
}
]
In Android side:
String jsonString3 =
"[
" +
" {
" +
" \"response\": [
" +
" {
" +
" \"1999\": \"200\",
" +
" \"2000\": \"210\",
" +
" \"2007\": \"240\"
" +
" }
" +
" ]
" +
" }
" +
"]";
try{
JSONArray array = new JSONArray(jsonString3);
JSONArray priceArray = array.getJSONObject(0).getJSONArray("response");
Log.d(LOG_TAG,"Price Array = " + priceArray.toString());
for (int i = 0; i < priceArray.length(); i++){
Log.d(LOG_TAG, "1999 = " + priceArray.getJSONObject(i).getString("1999"));
Log.d(LOG_TAG, "2000 = " + priceArray.getJSONObject(i).getString("2000"));
Log.d(LOG_TAG, "2007 = " + priceArray.getJSONObject(i).getString("2007"));
}
}catch (JSONException ex){
Log.e(LOG_TAG, ex.toString());
}
Suppose you received jsonData from your PHP api. I hope this helps you:
boolean success = false;
int temp= 0;
try {
JSONObject jsonobject = new JSONObject(jsonData);
success = jsonobject.getBoolean("success");
if (success) {
// Locate the NodeList name
JSONArray jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
temp = i+1;
jsonobject = jsonarray.getJSONObject(i);
String id = jsonobject.getString("FIELD"+temp);
String values = jsonobject.getString("value");
Log.e(LOG_TAG, "FIElD"+temp+": "+id);
Log.e(LOG_TAG, "Value: "+values);
}
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
Try this:
String jsonString3 =
"[
" +
" {
" +
" \"response\": \"\",
" +
" \"FIELD1\": [
" +
" {
" +
" \"value\": \"A\"
" +
" }
" +
" ],
" +
" \"FIELD2\": [
" +
" {
" +
" \"value\": \"B\"
" +
" }
" +
" ],
" +
" \"FIELD3\": [
" +
" {
" +
" \"value\": \"C\"
" +
" }
" +
" ],
" +
" \"FIELD4\": [
" +
" {
" +
" \"value\": \"D\"
" +
" }
" +
" ]
" +
" }
" +
"]";
try{
JSONArray data = new JSONArray(jsonString3);
JSONObject response = data.getJSONObject(0);
Iterator<?> keys = response.keys();
while( keys.hasNext() ){
String key = (String)keys.next();
if (response.get(key) instanceof JSONArray)
{
JSONArray aArray = (JSONArray) response.getJSONArray(key);
for (int i = 0; i < aArray.length(); i++){
Log.d(LOG_TAG, "item = " + key);
Log.d(LOG_TAG, "it's value = " + aArray.getJSONObject(i).getString("value"));
}
}
}
}catch (JSONException ex){
Log.e(LOG_TAG, ex.toString());
}
But, my JSON is hard coded. If you receive your JSON from PHP api, you should sum up with a solution like this code (I got my jsonData from my api):
String jsonData = apiCaller(params);
boolean success = false;
int temp= 0;
try {
JSONObject jsonobject = new JSONObject(jsonData);
success = jsonobject.getBoolean("success");
if (success) {
// Locate the NodeList name
JSONArray jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
temp = i+1;
jsonobject = jsonarray.getJSONObject(i);
String id = jsonobject.getString("FIELD"+temp);
String values = jsonobject.getString("value");
Log.e(LOG_TAG, "FIElD"+temp+": "+id);
Log.e(LOG_TAG, "Value: "+values);
}
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}