json输出中出现“Disallowed Key Characters”错误

时间:2016-06-15 03:31:33

标签: java json bufferedreader inputstreamreader

我正在尝试执行API并尝试获得json响应,但我的"Disallowed Key Characters"错误为bf.readLine()

以下是我正在尝试使用的代码。但是当我在Web浏览器中运行请求url时,我得到的响应没有问题。但是通过使用java代码,我无法提取数据。请帮忙

String uri = "http://192.168.77.6/Ivr_ABN_API/?id?id="
            + mobile;
    URL url;
    Gson json = null;
    try {
        url = new URL(uri);
        json = new Gson();

        HttpURLConnection connection;
        access_token = db.getAccessTokenFromDB();
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        System.out.println("URL:" + uri);

        connection.setRequestProperty("Content-Type", "application/json");
        int status = connection.getResponseCode();
        resCode = Integer.toString(status);


                InputStream in = connection.getInputStream();
                BufferedReader bf = new BufferedReader(
                        new InputStreamReader(in));
                System.out.println("bf.readLine() - "+bf.readLine());

                while ((output = bf.readLine()) != null) {
                    JSONObject obj = new JSONObject(output);
                    System.out.println("output is "+output);
                    resCode = obj.getString("resCode");
                    resDesc = obj.getString("COUNT");

                }


        connection.disconnect();

2 个答案:

答案 0 :(得分:0)

试试这个

BufferedReader in = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));

而不是

 BufferedReader bf = new BufferedReader(new InputStreamReader(in));

因为您需要添加编码技术,BufferedReader能够以适当的格式对任何特殊字符进行编码。

希望这对你有所帮助。

谢谢。

答案 1 :(得分:0)

我还找到了问题和发布的解决方案供您参考。

HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json");
        int status = connection.getResponseCode();
BufferedReader bf = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));