org.json.JSONException:类型java.lang.String成功时的值0无法转换为JSONArray

时间:2014-10-31 07:42:56

标签: java php android json

我尝试使用AsyncHttpClient API从php访问数据数组。我什么时候尝试获取数据异常:

 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();
            }
         }
       });

有人可以帮我解决这个问题吗?我最近一天都在努力解决这个问题。所以似乎有类似的问题,但不是确切的问题。

谢谢!

2 个答案:

答案 0 :(得分:1)

看起来你的意思是:

if (response.getInt("success") == 1) {
    JSONArray sucessdetails = response.getJSONArray("data");     

    for (int i = 0; i < sucessdetails.length(); i++)  {
        JSONObject successObject = sucessdetails.getJSONObject(i);

        Log.e("Value","Of success"+successObject);
    }
} else {
    Log.e("Status", "Failed");
}

但是,一般情况下,使用HTTP状态代码指示成功和失败比通过向JSON对象添加字段更好。

答案 1 :(得分:1)

@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) 
{
    try {
        // Checking for STATUS tag
        int success = response.getInt("success");
        Log.d("test", success);

        // extract all the data in the user JSON Array
        if (success == 1) {
            JSONArray sucessdetails = response.getJSONArray("data");
            for (int i = 0; i < sucessdetails.length(); i++) 
            {
                JSONObject successObject = sucessdetails.getJSONObject(i);

                Log.e("Value","Of success"+successObject);
            }
        } else {
            //You can use a toast here to indicate the failure status from server
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

如果您能记录响应,那么它总是很好,因此您可以看到JSON的外观。这使您可以更轻松地从JSON中提取数据。