I apologize in advance if this turns out to be an incorrect code, but nothing was better to “collect”.
DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet getRequest = new HttpGet( "http:"); getRequest.setHeader("Accept", "application/json"); // Use GZIP encoding getRequest.setHeader("Accept-Encoding", "gzip"); // try { HttpResponse response = (HttpResponse) httpclient .execute(getRequest); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); Header contentEncoding = response .getFirstHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { instream = new GZIPInputStream(instream); } // convert content stream to a String String result = readStream(instream); instream.close(); Log.i("JSON", result); TextView view = (TextView) findViewById(R.id.result); view.setText(result); JSONObject jsonObject = new JSONObject(result); } } catch (Exception e) { e.printStackTrace(); } } private static String readStream(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }
I do not understand the reason. If IT can be corrected, tell me where to change. If not, then I have specific questions that I could not find anything intelligible ...
Thank you in advance!