2015-09-05 57 views
0

我在这里工作我的应用程序,其中m在android中接收Json。这是我发送Json的php脚本。在Android中解释收到的Json

<?php 
require_once 'DB_connect.php'; 
class populatelist{ 
    private $con; 
    private $conn; 

    function __construct() 
    { 
     $this->con = new DB_connect(); 
     $this->conn = $this->con->connectWithRestaurant(); 
    } 
    function selectallfields(){ 
     $query = "SELECT * FROM `restaurant_time` LIMIT 50"; 
     $result = $this->conn->query($query); 
     if($result->num_rows >0) 
     { 
      while($record = $result->fetch_assoc()) 
      { 
       $response['resname'] = $record['Restaurant_name']; 
       $response['restadd'] = $record['Address']; 
       $response['resttime'] = $record['Waiting_time']; 
       $response['images'] = $record['Logo']; 
       echo json_encode($response); 
      } 
      echo json_encode($response); 
     } 
    } 
} 


$function = new populatelist(); 
$function->selectallfields(); 

?> 

这里是我的Android代码接受这个JSON请求。这是负责发送数据到我创建的自定义适配器。

class Jsonfetch extends AsyncTask<String,Void,ListView>{ 

    @Override 
    protected ListView doInBackground(String... params) { 
     try { 
      URL url = new URL("http://172.16.16.88/orderspot/populatelist.php"); 
      HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setAllowUserInteraction(false); 
      InputStream inputStream = httpURLConnection.getInputStream(); 



      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
      String temp; 
      StringBuilder stringBuilder = new StringBuilder(); 
      while((temp = bufferedReader.readLine())!=null){ 
       stringBuilder.append(temp); 
      } 
      String JsonResponse = stringBuilder.toString(); 
      Log.i("JsonResposne",JsonResponse); 
      try { 
       JSONArray new_array =new JSONArray(JsonResponse); 
       //int count; 

       for(int i = 0, count = new_array.length();i<count;i++){ 
        JSONObject jsonObject = new_array.getJSONObject(i); 
        list.add(new ListModel(jsonObject.getString("resname"),jsonObject.getString("restadd"),jsonObject.getString("resttime"),jsonObject.getString("images"))); 
       } 
       final customAdapter myadapter = new customAdapter(getApplicationContext(),list); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          listView.setAdapter(myadapter); 

         } 
        }); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return listView; 
    } 
} 

我正好不知道这个代码有什么问题。但也许,我得到了无效格式的Json。 这是我收到的Json。

{"resname":"Sankalp","restadd":"Infocity","resttime":"South Indian","images":"25"}{"resname":"South Cafe","restadd":"Infocity","resttime":"South Indian","images":"20"}{"resname":"Uncle Sam","restadd":"Infocity","resttime":"Pizza","images":"15"}{"resname":"Dangee Dums","restadd":"Infocity","resttime":"Dessert","images":"10"}{"resname":"Fresh Roast","restadd":"Infocity","resttime":"Cafe","images":"5"}{"resname":"Cafe Natrani","restadd":"Infocity","resttime":"Cafe","images":"30"}{"resname":"Chocolate Room","restadd":"Infocity","resttime":"Chocolate","images":"0"}{"resname":"Subway","restadd":"Infocity","resttime":"Sandwich","images":"4"}{"resname":"Jai Bhawani","restadd":"Infocity","resttime":"Breakfast","images":"5"}{"resname":"Jai Bhawani","restadd":"Infocity","resttime":"Breakfast","images":"5"} 

回答

0
$response = array(); 

while($record = $result->fetch_assoc()) 
{ 
    $restaurant = array(); 
    $restaurant['resname'] = $record['Restaurant_name']; 
    $restaurant['restadd'] = $record['Address']; 
    $restaurant['resttime'] = $record['Waiting_time']; 
    $restaurant['images'] = $record['Logo']; 
    $response[] = $restaurant; 
} 
echo json_encode($response); 
+0

无论是否有该行,JSON都是无效的。这不是问题。 – Breavyn

+1

需要upvote复制粘贴我的答案.... – Breavyn

+0

为什么是接受的答案,我的复制粘贴 - 有一个变量后,我把它叫出来重新命名....人们很好 – Breavyn

3

你来自服务器的响应是在JSONObject格式。不是JSONArray

修复你的php。

$response = array(); // JSONArray container 

while($record = $result->fetch_assoc()) { 
    // Build each JSONObject with your desired key names 
    $namedRecord = array(); 
    $namedRecord['resname'] = $record['Restaurant_name']; 
    $namedRecord['restadd'] = $record['Address']; 
    $namedRecord['resttime'] = $record['Waiting_time']; 
    $namedRecord['images'] = $record['Logo']; 

    // Insert each object into the array 
    array_push($response, $namedRecord); 
} 

// Output the array of objects 
echo json_encode($response); 

您要创建JSONObjects应此格式的JSONArray

[{key:value,key1:value1,...},{anotherObjectKey:anotherObjectValue}] 
+0

它说“空”。 –

+0

啊,什么说null? – Breavyn

+0

对不起,当时我没有初始化数组,所以它显示为空。但是现在在初始化之后,它显示“[]”。并添加到您的答案,不应该$回应是第一个参数? –

2

打印您在日志JSON响应并验证它..

http://jsonlint.com/

您的回应是不是一个有效的JSON,它包含了许多一个JSONObjects这应该是逗号状分离物,他们不是,并且它们全部应该包含在JSONArray中。请格式正确。

检查如何在PHP版本JSONArray此链接,

http://alvinalexander.com/php/php-json_encode-convert-array-to-json-example

你的反应应该是,

"rstaurants" :[ 
{ 
"resname": "Sankalp", 
"restadd": "Infocity", 
"resttime": "South Indian", 
"images": "25" 
}, 
{ 
"resname": "South Cafe", 
"restadd": "Infocity", 
"resttime": "South Indian", 
"images": "20" 
}, 
{ 
"resname": "Uncle Sam", 
"restadd": "Infocity", 
"resttime": "Pizza", 
"images": "15" 
}, 
{ 
"resname": "Dangee Dums", 
"restadd": "Infocity", 
"resttime": "Dessert", 
"images": "10" 
}, 
{ 
"resname": "Fresh Roast", 
"restadd": "Infocity", 
"resttime": "Cafe", 
"images": "5" 
} 
] 

目前,它是,

{ 
"resname": "Sankalp", 
"restadd": "Infocity", 
"resttime": "South Indian", 
"images": "25" 
} 
{ 
"resname": "South Cafe", 
"restadd": "Infocity", 
"resttime": "South Indian", 
"images": "20" 
} 
{ 
"resname": "Uncle Sam", 
"restadd": "Infocity", 
"resttime": "Pizza", 
"images": "15" 
} 
{ 
"resname": "Dangee Dums", 
"restadd": "Infocity", 
"resttime": "Dessert", 
"images": "10" 
} 
{ 
"resname": "Fresh Roast", 
"restadd": "Infocity", 
"resttime": "Cafe", 
"images": "5" 
} 

让我知道它是否适合你... 并做标记它作为答案,以便它对其他人有用...

+0

你能指导我吗? –

+0

好! bt现在我应该怎么做我的代码? –

+0

@Milind通过粘贴我的答案; P – Breavyn