Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

        }
like image 345
dmaprasad Avatar asked Sep 07 '25 12:09

dmaprasad


1 Answers

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.

like image 128
Bruce H Avatar answered Sep 10 '25 05:09

Bruce H