0

I added this header in my base request: "Accept-encoding", "gzip"

This now results in a MalformedJsonException urging me to use JsonReader.setLenient(true)

I've modified my code to this to support that:

JsonReader jsonReader = new JsonReader(new StringReader(myStringReader with json string));
jsonReader.setLenient(true);
JsonParser parser = new JsonParser();
JsonObject object = parser.parse(jsonReader).getAsJsonObject();

I'm still getting the same error, thoughts anyone?

AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56

1 Answers1

1

Couple things -

  1. If you're setting the GZIP header, that tells the server you're contacting that you're able to unzip a gzip'd response. Likely you're getting back the compressed response, and trying to parse it as json - you need to decompress it first.

  2. If you're using HttpUrlConnection, it will automatically set the gzip header and unzip it for you. You can confirm this by proxying your app and looking at the http requests it makes.

  3. A funny thing about HttpUrlConnection is that while it will set the gzip header and decompress for you, if you set the gzip headers yourself, it will not. It then will rely on you to decompressed it.

So in short, either don't set the header at all and rely on HttpUrlConnection to do it all for you, or you must decompress the response using the GZIPInputStream class prior to your json code, see this answer for an in-depth explanation:

How can I Zip and Unzip a string using GZIPOutputStream that is compatible with .Net?

Community
  • 1
  • 1
Sam Dozor
  • 40,335
  • 6
  • 42
  • 42