Reputation: 5954
I trying access array of data from a php using AsyncHttpClient API. When ever I try to get the data I exception:
org.json.JSONException: Value 0 at success of type java.lang.String cannot be converted to JSONArray
I am not sure what is wrong here.
If $response["success"] = 1 then I would like to do something if not I want to toast a value for which I am trying to access success value.
Here is my PHP file:
<?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);
}
?>
And my Android code:
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();
}
}
});
Can somebody help me fix this I am been working on it for last 1 day. SO seems to have similar issues but not the exact problem.
Thanks!
Upvotes: 0
Views: 217
Reputation: 1744
@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();
}
}
Its always good if you could Log you response and hence you can see how the JSON looks like. This makes it easier for you to extract the data from the JSON.
Upvotes: 1
Reputation: 97120
Looks like you mean to do this:
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");
}
In general, however, it's better to indicate success and failure using HTTP status codes than by adding fields to your JSON object.
Upvotes: 1