我試圖使用AsyncHttpClient API訪問來自php的數據數組。當我試圖獲得我的例外數據時:org.json.JSONException:成功時的值爲0的java.lang.String不能轉換爲JSONArray
org.json.JSONException: Value 0 at success of type java.lang.String cannot be converted to JSONArray
我不確定這裏有什麼問題。
如果$ response [「success」] = 1,那麼我想做一些事情,如果不是,我想烤一個值,我試圖訪問成功價值。
這裏是我的PHP文件:
<?php
include("config.php");
$response = array();
if (isset($_GET['userID']))
{
$userID = $_GET['userID'];
// mysql inserting a new row
$result = //MY QUERY GOES HERE
// check for empty result
if (mysql_num_rows($result) > 0)
{
$response["data"] = array();
while ($row = mysql_fetch_array($result))
{
// temp user array
$detail = array();
$detail["userID"] = $row["username"];
$detail["password"] = $row["password"];
array_push($response["data"], $detail);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}
else
{
// no products found
$response["success"] = 0;
// echo no users JSON
echo json_encode($response);
}
}
else
{
// no products found
$response["success"] = 0;
// echo no users JSON
echo json_encode($response);
}
?>
而且我的Android代碼:
AsyncHttpClient client = new AsyncHttpClient();
client.get(uploadWebsite, requestParams, new JsonHttpResponseHandler()
{
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response)
{
try
{
JSONArray sucessdetails = response.getJSONArray("success");
for (int i = 0; i < sucessdetails.length(); i++)
{
JSONObject successObject = sucessdetails.getJSONObject(i);
Log.e("Value","Of success"+successObject);
}
}
catch (JSONException e)
{
Log.e("ERROR","E"+e);
e.printStackTrace();
}
}
});
有人可以幫我解決這個問題,我一直在努力的最後1天。 SO似乎有類似的問題,但不是確切的問題。
謝謝!
嘗試記錄JSON響應並在此處進行更新。看着JSON,你會知道你在做什麼是錯的。帶有標籤'「success」'的元素不是JSON數組 – codePG 2014-10-31 07:45:25
您期待的是JSONArray,但是您將在JSON中獲得一個字符串您是否檢查過JSON – 2014-10-31 07:45:31
@PramodYadav:$ response [data]的結果是如果我只是改變response.getJSONArray(「success」); to response.getJSONArray(「data」);怎麼樣? – TheDevMan 2014-10-31 07:50:34