I sent a request for question with no parameters to https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow
I received a response of 9764 bytes stored in StringBuffer.
The StringBuffer was converted to byte[] to deCompress. GZIPInputStream threw an exception:
Not in GZIP format.
Is it necessary to set parameters in the request to get a response in GZIP format? If not, how to inflate the response?
public class MyConnect { private static final String GET_URL = "https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow"; private static void sendGET() throws IOException { URL obj = new URL(GET_URL); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK){ BufferedReader in = new BufferedReader(new InputStreamReader( con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while((inputLine = in.readLine()) != null){ response.append(inputLine); } System.out.println("len: "+ response.length()); System.out.println("bytes: "+ response.toString().getBytes().length); in.close(); String s = deCompress(response.toString().getBytes()); if (s != null) System.out.println(s); else System.out.println("response is null"); }else { //ToD: error handling. System.out.println(responseCode); } } public static void main(String[] args){ try{ MyConnect.sendGET(); }catch(IOException e){ System.out.println("CONNECTION FAILED"); }finally{ } } private static String deCompress(byte[] str) throws IOException{ StringBuilder sb = null; try{ ByteArrayInputStream bis = new ByteArrayInputStream(str); GZIPInputStream gis = new GZIPInputStream(bis); // *** exception : not in GZIP format BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8")); sb = new StringBuilder(); String line; while ((line = br.readLine()) != null){ sb.append(line); } br.close(); gis.close(); bis.close(); }catch(IOException e){ e.printStackTrace(); }finally{ ; } return sb.toString(); } }