获取HTML响应而不是Json响应

时间:2016-06-15 09:10:56

标签: java json httpurlconnection

我正在获取HTML响应而不是JSON响应。我正在使用以下代码,我收到的HTML响应为bf.readLine()。以下代码中是否存在任何问题或此API问题?

String uri = "http://192.168.77.6/Ivr_ABN_API/?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);
        System.out.println("status is " + status);
        InputStream in = connection.getInputStream();
        System.out.println("inputStreamer " + in);
        BufferedReader bf = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        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");

        }

2 个答案:

答案 0 :(得分:7)

也许尝试发送标题Accept:application / json

如果这不起作用,请查看API的文档,看看是否还有其他内容要发送给json。

答案 1 :(得分:0)

java

将Request Property设置如下:

con.setRequestProperty("Accept","application/json")

它将解决您面临的问题。