Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 试图将数据转换为字符串时出现JSONException_Android_Json - Fatal编程技术网

Android 试图将数据转换为字符串时出现JSONException

Android 试图将数据转换为字符串时出现JSONException,android,json,Android,Json,所以有一个PHP文件,我在其中向我的android设备发送一个数组。在我的设备接收到数据后,它将其转换为字符串,然后我尝试获取用PHP创建的assoc数组索引。以下是以前的PHP代码示例: if(mysql_num_rows($query)){ $output = array('message' => 'Group added to your database', 'succes' => 'true'); }else{ $output = array('messag

所以有一个PHP文件,我在其中向我的android设备发送一个数组。在我的设备接收到数据后,它将其转换为字符串,然后我尝试获取用PHP创建的assoc数组索引。以下是以前的PHP代码示例:

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));
以下是我的Android项目中的代码:

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

您试图从表示JSONObject的字符串创建JSONArray,应该执行以下操作:

try{
    JSONObject json_data=JSONObject(result);
    String getMessage = json_data.getString("message");
    showToast(getMessage);
}

Json异常中给出了哪些详细信息?同样,succes的拼写是success。错误(JSONException):org.json.JSONException:Value{“message”:“Group added to your database”,“success”:“true”}类型的org.json.JSONObject无法转换为jsonarrayeah。我也看到了这一点,但不太知道如何解决这个问题。Thanx:)
try{
    JSONObject json_data=JSONObject(result);
    String getMessage = json_data.getString("message");
    showToast(getMessage);
}