Reputation: 1185
So there is a PHP file where I'm sending an array to my android device. After my device receives the data, it converts it to a string and after that I'm trying to get the assoc array index that was made with PHP. Here is a sample of the PHP code from before:
if(mysql_num_rows($query)){
$output = array('message' => 'Group added to your database', 'succes' => 'true');
}else{
$output = array('message' => 'Wrong username/password combination', 'succes' => 'false');
}
print(json_encode($output));
and here is how the code in my Android project looks like:
protected void onPostExecute(String result) {
dialog.dismiss();
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i = 0; i < jArray.length(); i++){
json_data = jArray.getJSONObject(i);
String getMessage = json_data.getString("message");
showToast(getMessage);
}
}
catch(JSONException je){
Log.e("log_tag", "Error (JSONException): "+je.toString());
showToast(result);
}
catch (ParseException pe) {
pe.printStackTrace();
}
}
Error log:
01-08 16:35:55.896: E/log_tag(647): Error (JSONException): org.json.JSONException: Value {"message":"Group added to your database","success":"true"} of type org.json.JSONObject cannot be converted to JSONArray
01-08 16:35:56.186: W/TextLayoutCache(647): computeValuesWithHarfbuzz -- need to force to single run
Upvotes: 0
Views: 664
Reputation: 137282
You are trying to create JSONArray from string that represents JSONObject, you should do:
try{
JSONObject json_data=JSONObject(result);
String getMessage = json_data.getString("message");
showToast(getMessage);
}
Upvotes: 1