无法将Java.lang.Integer类型的值0转换为JSONObject
我正在尝试为我的应用程序执行以下代码:无法将Java.lang.Integer类型的值0转换为JSONObject,java,android,json,Java,Android,Json,我正在尝试为我的应用程序执行以下代码: JSONObject json = jsonParser.makeHttpRequest(url_kickit_connect, "POST", params); // check log cat for response Log.d("Create Response", json.toString()); try {
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());
}
我从MySQL side.php获取的JSON值:
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
如果我尝试将值或数据类型更改为字符串等,则错误消息只会更改为告知值或数据类型。
如何解决此问题?0不是有效的json。服务器应该返回类似于{response:0}的内容。对于这种情况要将响应作为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);
}
你也可以发布json值吗?谢谢,我完全误解了代码,你的回答对我帮助很大!