“Disallowed Key Characters” error in json output

狂风中的少年 提交于 2019-12-13 05:50:53

问题


I'm trying to execute API and try to get json response, but I'm getting error as "Disallowed Key Characters" for bf.readLine().

Following is the code that I'm trying to use. But when I run the request url in web browser I'm getting response without issue.But by using java code I'm unable to extract data. Please help

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

回答1:


try this

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

instead of

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

because you need to add encoding technique by which a BufferedReader able to encode any special character in proper format.

Hope this might help you.

Thank You.




回答2:


I also found a solution for the question and posting for your reference.

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


来源:https://stackoverflow.com/questions/37825618/disallowed-key-characters-error-in-json-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!