我在這裏工作我的應用程序,其中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"}
無論是否有該行,JSON都是無效的。這不是問題。 – Breavyn
需要upvote複製粘貼我的答案.... – Breavyn
爲什麼是接受的答案,我的複製粘貼 - 有一個變量後,我把它叫出來重新命名....人們很好 – Breavyn