简体   繁体   中英

Getting HTML response instead of Json response

I'm getting HTML response instead of JSON response. I'm using following code and I'm receiving HTML response as bf.readLine(). Is there any issue in following code or is this API issue?

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

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.

For java

Set The Request Property as the following:

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

It will solve the issue you are facing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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