"Disallowed Key Characters" json 输出中的错误

"Disallowed Key Characters" error in json output

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

以下是我正在尝试使用的代码。但是当我在网络浏览器中 运行 请求 url 时,我通过使用 java 代码得到了没有 issue.But 的响应,我无法提取数据。请帮助

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

试试这个

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

而不是

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

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

希望这对您有所帮助。

谢谢。

我也找到了问题的解决方法发帖给大家参考

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