简体   繁体   English

获取HTML响应而不是Json响应

[英]Getting HTML response instead of Json response

I'm getting HTML response instead of JSON response. 我正在获取HTML响应而不是JSON响应。 I'm using following code and I'm receiving HTML response as bf.readLine(). 我正在使用以下代码,并且收到bf.readLine()的HTML响应。 Is there any issue in following code or is this API issue? 以下代码是否存在任何问题,或者此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");

        }

Perhaps try sending the header Accept: application/json 也许尝试发送标头Accept:application / json

If that doesn't work, then review the documentation for the API and see if there's something else you should be sending to return json. 如果这不起作用,请查看API文档,看看是否还有其他内容需要发送以返回json。

For java 对于java

Set The Request Property as the following: 将请求属性设置为以下内容:

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

It will solve the issue you are facing. 它将解决您面临的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM