dongyuying1507 2012-03-12 07:41
浏览 37
已采纳

如何通过PHP脚本推送的JSONencoded字符串在Android站点中检索JSON数组数据?

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 ?

  • 写回答

1条回答 默认 最新

  • douhe6181 2012-03-12 08:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。