1

I am hitting the following url from my android device.

URL : https://www.googleapis.com/youtube/v3/search?key=MY_KEY&channelId=UCoWgc1mqe-bcfb_lem7EyOg&part=snippet,id&order=date&maxResults=50

Response: ������������������]�r�8�}ϯ@e?䋕�}ɗ j����%OO(@�q3���*b�a"�g�o�K��%Y]�em�,uGd8m�ātu��]������/�c���W�_����B���M��;v�������q:��fA�[8KR�����lZ?[t]�඘JL���`�������G1����h䬑��T[}E�y�]qt�_xl��.��e/v�o���H

For getting the YouTube Videos List.It was working fine before but sometimes its response is not valid while hitting from browsers it works well.

Please help me !!

Ejaz Ahmad
  • 614
  • 5
  • 12
  • Inspect it with a tool like `Fiddler` or `Wireshark` in order to inspect the content-encoding http header. The first thing that comes in my mind is, that the response you get might be compressed into `gzip`, `bzip` or `deflate`. In this case you have to decompress it like you have to do it with a `zip` file. If the response is compressed you have to decompress it first in your app. – cramopy Sep 21 '17 at 20:03
  • Why it was working before ? and also how it is working on browsers ? My apps are on playstore and they were working fine. Suddenly apps stops working and in logs above stupid thing is showing with 200 response code. – Ejaz Ahmad Sep 21 '17 at 20:12
  • please read my comment again... Please provide the value of the `Content-Encoding` header of the response in order to help you. – cramopy Sep 22 '17 at 05:04
  • the browser do automatically decode the value, these are Specialized for this. – cramopy Sep 22 '17 at 05:06
  • X-Origin application/json; charset=UTF-8 gzip nosniff SAMEORIGIN 1; mode=block GSE quic=":443"; ma=2592000; v="39,38,37,35" chunked These values are going in the header – Ejaz Ahmad Sep 22 '17 at 05:35
  • as you can see, your response was compressed using `gzip`. Just decompress it and you have the Jason data you wanted. – cramopy Sep 22 '17 at 05:41
  • if this is problem why YouTube change response and they have not notified us – Ejaz Ahmad Sep 22 '17 at 05:48
  • Now i have to code it and upload a new build ..its means i will lost my thousands of users – Ejaz Ahmad Sep 22 '17 at 05:49

2 Answers2

1

I found the solution i was using the loop j library (for http request) old version

just changes

compile 'com.loopj.android:android-async-http:1.4.8'

to

compile 'com.loopj.android:android-async-http:1.4.9'

it solved my problem

Ejaz Ahmad
  • 614
  • 5
  • 12
0

Please see my related question here (but in C#) and the answers on it in order to understand what I mean. Then you have to search example fordecompress gzip android and look at the existing resources to accomplish this.

Please keep in mind, that the API does not always send the response in gzip-format or not. So please do a check if the response is really in gzip.

For the plain decompression you can use the following method:

public static String decompress(byte[] compressed) throws IOException {
    final int BUFFER_SIZE = 32;
    ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
    GZIPInputStream gis = new GZIPInputStream(bis, BUFFER_SIZE);
    StringBuilder s = new StringBuilder();
    byte[] data = new byte[BUFFER_SIZE];
    int bytesRead;
    while ((bytesRead = gis.read(data)) != -1) {
        s.append(new String(data, 0, bytesRead));
    }
    gis.close();
    bis.close();
    return s.toString();
}

(this code was taken from Vyshnavi's answer)

cramopy
  • 3,459
  • 6
  • 28
  • 42