我在这里处理我的应用程序,其中m在android中接收Json。这是我发送Json的PHP脚本。
<?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();
?>
这是我接受此Json请求的Android代码。这负责将数据发送到我创建的自定义适配器。
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 :(得分: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);
您正在尝试创建一个JSONArray
JSONObjects
,其格式应该是这样的。
[{key:value,key1:value1,...},{anotherObjectKey:anotherObjectValue}]
答案 1 :(得分:2)
在日志中打印您的JSON响应并在其上验证..
您的响应不是有效的JSON,它包含许多JSONObjects,它们应该是逗号分隔的,它们不是,并且所有JSONObject都应该包含在JSONArray中。请正确格式化。
检查此链接,了解如何在php中构建JSONArray,
http://alvinalexander.com/php/php-json_encode-convert-array-to-json-example
你的回答应该是,
FreeLibraryAndExitThread
目前正是,
"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"
}
]
让我知道它是否适合你...... 并将其标记为答案,以便对其他人有用......
答案 2 :(得分: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);