我试图使用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