2

I want to get data using HTTP GET method. But response is wrong

java.net.ProtocolException: Unexpected status line: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

How to fix it?

public  JSONArray GetPost_sport(String token, String sport_type){

    JSONArray jsonArray = null;

    try {
        URL url = new URL("http://o-two-sport.com/api/posts/?sport_type="+sport_type);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization","Token " +token);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Connection", "close");
        conn.connect();

        InputStream is = conn.getInputStream();
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        StringBuilder responseStrBuilder = new StringBuilder();

        String inputStr;
        while ((inputStr = streamReader.readLine()) != null)
            responseStrBuilder.append(inputStr);

        jsonArray = new JSONArray(responseStrBuilder.toString());

    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }

    return jsonArray;
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Hye Rim Hyeon
  • 47
  • 1
  • 7
  • the HTTP response is not json str, it return error info by html. you need confirm which type content the HTTP response is when you request is invalid. – neuo Apr 12 '17 at 06:18

1 Answers1

0

You are retrieving a usual HTML page as response, which is not JSON - ready. Find an API of this site to retrieve JSON content type.

I suppose you should add format=json to your URL:

new URL("http://o-two-sport.com/api/posts/?format=json&sport_type="+sport_type);
Ivan Pronin
  • 1,768
  • 16
  • 14