JSONArray不能转换为JSONObject


JSONArray cannot be converted to JSONObject

我得到以下错误:

Error parsing data org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject

下面是Android和PHP代码:

private void eventUpdatePoint(String eventStatus,int lat,int lon){
    JSONParser jsonParserEUP = new JSONParser(); 
        try
        {   
            List<NameValuePair> params = new ArrayList<NameValuePair>(); 
            params.add(new BasicNameValuePair("longtitude", Integer.toString(lon))); 
            params.add(new BasicNameValuePair("latitude", Integer.toString(lat)));
            params.add(new BasicNameValuePair("eventstatus", eventStatus));
            String url_updatePoint = "http://10.0.2.2/android_connect/event_update_point.php";
            JSONObject json = jsonParserEUP.makeHttpRequest(url_updatePoint,"POST", params);
            Log.d("Response", json.toString()); 
        }
        catch(Exception e)
        {
            e.printStackTrace();
            Log.d("ERROR:","ERROR:"    + e.getClass().getName() + ":" + e.getMessage());
        }
    }
php代码:

$response = array(); if (isset($_POST['longtitude']) && isset($_POST['latitude']) &&isset($_POST['eventstatus'])) { 
    $longtitude = $_POST['longtitude']; 
    $latitude = $_POST['latitude']; 
    $eventstatus = $_POST['eventstatus'];
    $e_id=0;
    $score=0;

    require_once __DIR__ . '/db_connect.php'; 
    $db = new DB_CONNECT(); 
    $result = mysql_query("SELECT id FROM cordinates WHERE longtitude=$longtitude AND latitude=$latitude") or die(mysql_error()); 
    while ($row = mysql_fetch_array($result)) { 
            $e_id = $row["id"];  
        } 
    $result = mysql_query("SELECT score FROM point WHERE e_id=$e_id") or die(mysql_error()); 
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_array($result)) { 
            $score = $row["score"];  
        } 
        if($eventstatus=="inc")
        {   
            $score+=10;
            $result = mysql_query("UPDATE point SET score=$score where e_id=$e_id") or die(mysql_error());  
            $response["success"] = 1; 
             $response["message"] ="Score point increased";
        }
        else if($eventstatus=="dec")
        {   
            $score-=10;
            $result = mysql_query("UPDATE point SET score=$score where e_id=$e_id") or die(mysql_error());
            $response["success"] = 0; 
             $response["message"] ="Score point decreased";
        }
    } 
    echo json_encode($response); 

我做错了什么?

您正在使用的web服务返回一个JSONArray。您必须使用JSONArray类型的对象来存储它。你不能使用JSONObject。

改变这一行:

JSONObject json = jsonParserEUP.makeHttpRequest(url_updatePoint,"POST", params);

…:

JSONArray json = jsonParserEUP.makeHttpRequest(url_updatePoint,"POST", params);

记录您的响应。如果它以"["开头,以"]"结尾,则必须将其存储在类型为"JSONArray"的变量中。注意您的日志,您的响应必须是"JSONArray",因此只需将您的响应保存在类型为"JSONArray"的变量中。