Reputation: 443
If my query is successful then my php server returns json object like this
{"Success":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
if it fails the it returns jsonobject like this
{"fail":[
{"msg":"Fail reason"}
]}
i want to detect "success" and "fail" in android . I tried this, its not working, any better solution is welcome ...
JSONObject jsonResponse
if(jsonResponse.getBoolean("orderid"))
Upvotes: 1
Views: 61
Reputation: 1181
check the key you can:
JSONObject jsonResponse;
if(jsonResponse.has("Success")){
JSONArray data = jsonResponse.optJSONArray("Success");
//*********
}else{
String error = jsonResponse.optJSONArray("fail").optJSONObject(0).optString("msg");
}
Upvotes: 1
Reputation: 21
Change
if(jsonResponse.getBoolean("orderid"))
to
if(jsonResponse.has("Success"))
Upvotes: 1