JSONArray无法转换为JSONObject

时间:2013-03-24 17:44:27

标签: php android json

我收到以下错误:

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

我做错了什么?

2 个答案:

答案 0 :(得分:1)

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

更改此行:

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

......对此:

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

答案 1 :(得分:0)

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