【问题标题】:Value 0 of type Java.lang.Integer cannot be converted to JSONObjectJava.lang.Integer 类型的值 0 无法转换为 JSONObject
【发布时间】:2013-02-26 09:38:00
【问题描述】:

我正在尝试为我的应用执行此代码:

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 端获得的 JSON 值(.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

如果我尝试将值或数据类型更改为字符串等。错误消息只会更改为告知的值或数据类型。 我该如何解决?

【问题讨论】:

标签: java android json


【解决方案1】:

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

【讨论】:

  • 谢谢你,我完全误解了代码,你的回答对我帮助很大!
【解决方案2】:

要以 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);
}

【讨论】:

    猜你喜欢
    • 2015-12-02
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多