2013-02-26 97 views
1

我试图执行该代码为我的应用程序:值0类型为java.lang.Integer不能转换为JSONObject的

JSONObject json = jsonParser.makeHttpRequest(url_kickit_connect, 
        "POST", params); 

      // check log cat for response 
      Log.d("Create Response", json.toString()); 

      try { 
       int response = json.getInt(TAG_RESPONSE); 
       if (response == 0){ 
       Toast.makeText(getApplicationContext(), "email " +mail , Toast.LENGTH_SHORT).show(); 
       } 

       } 

       catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

虽然得到一个0的返回值,0应该被解析为这段代码中的JSONObject(以上我做标准Httppost和StringBuilder的):

try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

的JSON值,我从我的MySQL侧得到(.PHP):

else 
{ 
    $response = 0; 
    echo json_encode($response); 
} 

但我得到这个错误的logcat:

02-26 09:24:14.920: E/JSON Parser(619): Error parsing data org.json.JSONException: Value 0 of type java.lang.Integer cannot be converted to JSONObject 

如果我试图改变的值,或字符串数​​据类型等错误信息只是改变到告知值或数据类型。 我该如何解决这个问题?

+1

你能张贴JSON价值呢? – 2013-02-26 09:43:17

+0

看到这个http://stackoverflow.com/questions/11991971/what-should-i-do-to-solve-this-error – TheWhiteRabbit 2013-02-26 09:45:55

回答

3

0不是有效的json。服务器应该返回类似{"response": 0}这种情况

+0

谢谢你,我完全误解了代码,你的答案帮了我很多! – Werdli 2013-02-26 09:55:18

-1

要发送的响应作为JSON,添加此的PHP/MySQL的侧

else { 
    //creates an array 
    $result = array(); 
    //adds a json node for response 
    $result['response'] = 0; 
    //encode and echo out 
    echo json_encode($result); 
} 
相关问题