Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:15:43+00:00 2026-06-18T08:15:43+00:00

I have the following 2 methods in my codes. For now, i could read

  • 0

I have the following 2 methods in my codes. For now, i could read JSON from URL based on the following codes. However, how should i modify the codes so that I could get a gzip file, then decode it to JSON?

private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
}

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {

    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      Log.e("size of text", jsonText.length()+"");

      JSONObject json;
      if (jsonText.contains("</div>")) {
          json = new JSONObject(jsonText.split("</div>")[1]);
      } else {
          json = new JSONObject(jsonText);
      }
      return json;
    } finally {
      is.close();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:15:44+00:00Added an answer on June 18, 2026 at 8:15 am
    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    
        HttpUriRequest request = new HttpGet(url);
        request.addHeader("Accept-Encoding", "gzip");
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);
    
        InputStream instream = response.getEntity().getContent();
        org.apache.http.Header contentEncoding = response.getFirstHeader("Content-Encoding");
    
        JSONObject json = null;
        if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
            BufferedReader rd = new BufferedReader(new InputStreamReader(new GZIPInputStream(instream)));
            String jsonText = readAll(rd);
              if (jsonText.contains("</div>")) {
                  json = new JSONObject(jsonText.split("</div>")[1]);
              } else {
                  json = new JSONObject(jsonText);
              }
        }
        return json;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m after some C# code that will have the following methods that will return
I have following code for loading image from url in xml parsing endElement method
I have the following class which contains a hard coded URL that never changes:
I have following code and i want to pass additional parameter in JSON.getJSONfromURL() method
I have a the following generic methods: public static string Glue(string prefix, string value)
I have a simple ASP.NET hosted WCF Service that has several methods that all
I have been following this great tutorial and got my codes to working with
Its been a while I have ready Mcconnell's Code Complete . Now I read
I have a public readonly property that calculates its value based upon a couple
I have the following method in an App_Code/Globals.cs file: public static XmlDataSource getXmlSourceFromOrgid(int orgid)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.