String url = Config.DATA_URL+TempItem.toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.GET,url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
這是我在解析我的響應中的構造函數。 我是Android工作室的初學者,我不知道如何解決這個錯誤。我已經閱讀過其他論壇,我試圖實施無濟於事。我的JSON結果是java.lang.String類型的值連接無法轉換爲JSONObject
"result":[
{
"BusinessName":"KachangPuteh",
"AmountTotal":"100",
"RequiredTotal":"200",
"MaxTotal":"500"
}
]
}
private void showJSON(String response){
String name="";
String AmountTotal="";
String RequiredTotal = "";
String MaxTotal = "";
try {
JSONObject jsonObject = new JSONObject(response);
String results= jsonObject.getString(Config.JSON_ARRAY);
JSONArray result = new JSONArray(results);
JSONObject stallsData = result.getJSONObject(0);
name = stallsData.getString(Config.KEY_NAME);
AmountTotal = stallsData.getString(Config.KEY_AmountTotal);
MaxTotal = stallsData.getString(Config.KEY_MT);
RequiredTotal = stallsData.getString(Config.KEY_RT);
} catch (JSONException e) {
e.printStackTrace();
Log.e("error ",e.getMessage());
}
Stall.setText("Name:\t"+name+"\nAmountTotal:\t" +AmountTotal+ "\nMaxTotal:\t"+ MaxTotal);
}
這是我的JSONObject改變JSONArray。
編輯: 這是我的PHP文件
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
require_once('conn.php');
$sql = "SELECT * FROM business WHERE BusinessID='".$id."'";
$r = mysqli_query($conn,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"BusinessName"=>$res["BusinessName"],
"AmountTotal"=>$res["AmountTotal"],
"RequiredTotal"=>$res["RequiredTotal"],
"MaxTotal"=>$res["MaxTotal"]
)
$str = json_encode(array("result"=>$result)); $str=str_replace('​','',$str); $str=str_replace('‌','',$str); echo $srt;
echo json_encode(array("result"=>$result));
);
mysqli_close($conn);
}
這是我的配置文件。
public class Config {
public static final String DATA_URL = "http://192.168.1.2/retrieveone.php?id=";
public static final String KEY_NAME = "BusinessName";
public static final String KEY_AmountTotal = "AmountTotal";
public static final String KEY_RT = "RequiredTotal";
public static final String KEY_MT = "MaxTotal";
public static final String JSON_ARRAY = "result";
}
值
HahaHehe
可以確保,在響應你沒有得到HTML標籤。 – Rahul
我該如何解決它?如果提供代碼會有幫助 – HahaHehe