2014-10-31 69 views
0

我试图使用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似乎有类似的问题,但不是确切的问题。

谢谢!

+0

尝试记录JSON响应并在此处进行更新。看着JSON,你会知道你在做什么是错的。带有标签'“success”'的元素不是JSON数组 – codePG 2014-10-31 07:45:25

+0

您期待的是JSONArray,但是您将在JSON中获得一个字符串您是否检查过JSON – 2014-10-31 07:45:31

+0

@PramodYadav:$ response [data]的结果是如果我只是改变response.getJSONArray(“success”); to response.getJSONArray(“data”);怎么样? – TheDevMan 2014-10-31 07:50:34

回答

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对象。

+0

谢谢。得到它了。我的错。将查看HTTP状态代码。 – TheDevMan 2014-10-31 08:08:16

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中提取数据。

+0

非常感谢。我弄清楚了什么是错误。 – TheDevMan 2014-10-31 08:09:04

+0

它对你有帮助。快乐编码:) – codePG 2014-10-31 08:09:35

相关问题