cmh
cmh

Reputation: 271

How to retrieve JSON Array Data in Android site via JSONencoded string pushed by PHP Script?

I have written a PHP script below try to Post the JSON encoded string to Android device as below:

<?php
 // Create HostConnection
 $host = $_SERVER['HTTP_HOST'];  
 if ($host)
 {
    $data = array('HTTP_HOST' => $host);    
 }
 else
 {
    $data = array('HTTP_HOST' => "Not Available");
 }

 // Create Connection
 $link = mysql_connect('127.0.0.1:3306', 'root', 'xxxxxx');
 if ( $link != false )
 {
    //echo "mysql_connect success !";
    // Set Use UTF-8 String
    if (mysql_set_charset('utf8', $link)  && $db_selected = mysql_select_db("gw"))
    {
       // echo "mysql_set_charset() success<br />";       
       array_push($data, "SQL_DB", "gw");           
    } 
 }
 else
 {
   array_push($data, "SQL_DB", "Not Available");
 }       

 print_r(urldecode(json_encode($data)));
?>

Before post to Android device, I run this PHP script on the localhost(127.0.0.1) and got the result shown on the screen as:

{"HTTP_HOST":"127.0.0.1","0":"SQL_DB","1":"gw"}

Why there are 3 sets of data ? what I really want is 2 sets of data shown and prepare send to Android device on nest step. Where I was wrong ? If yes, what's the correct procedures to retrieve the encoded JSON array string on Android side ?

Upvotes: 0

Views: 219

Answers (1)

Pekka
Pekka

Reputation: 449555

You are not pushing arrays to the array, but single elements. You can't add an associative array value using the method you show.

Use

 $data["SQL_DB"] = "gw";     

instead.

Upvotes: 2

Related Questions