3

I'm trying to receive some data as a gzipped json, according to this answer by Jesse Wilson OkHttp automatically decompress and I don't have to do anything.

So my question is, Can I receive the gzipped Json like this? :

apiService.getMessages().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<MessagesGet>() {
                    @Override
                    public void onNext(MessagesGet messagesGet) {



                        Timber.d("GET MESSAGES DATA READY TO HANDLE");

                    }

                    @Override
                    public void onError(Throwable t) {

                        Timber.e("ERROR GETTING MESSAGES");

                    }

                    @Override
                    public void onComplete() {

                        Timber.e("GETMESSAGES COMPLETED");

                    }
                });

and

@Headers({
            "Content-Type: application/json;charset=utf-8",
            "Accept: application/json"
    })
    @GET("getmessages")
    Flowable<MessagesGet> getMessages();
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85

1 Answers1

0

As discussed..

Service interface. where you declare APIs

@Headers("Content-Type: application/x-gzip")
@GET(Apis.GET_PATH)
Single<Response<ResponseBody>> downloadGzip(@Path(value = "path", encoded = true) String path);

Retrofit + RxJava config

public <S> S createService(Class<S> serviceClass) {


    OkHttpClient.Builder mHttpClient = new OkHttpClient.Builder();
    mHttpClient.connectTimeout(60, TimeUnit.SECONDS);
    mHttpClient.readTimeout(60, TimeUnit.SECONDS);
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

    if (BuildConfig.DEBUG) {
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    } else {
        logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
    }

    mHttpClient.addInterceptor(logging);

    return new Retrofit.Builder()
            .baseUrl(baseURL)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(mHttpClient.build())
            .build()
            .create(serviceClass);

}

Trigger GZIP Download

ApiService apiService = serviceGenerator.createServiceForm(ApiService.class);

apiService.downloadGzip(apiEndPoint) // apiEndPoint is direct download URL of GZIP
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new SingleObserver<Response<ResponseBody>>() {
                @Override
                public void onSubscribe(Disposable disposable) {
                    AppLogger.i(LOGGER, "gzip download ->" + "subscribed");
                }

                @Override
                public void onSuccess(Response<ResponseBody> responseBodyResponse) {
                //responseBodyResponse contains gzip
                    AppLogger.i(LOGGER, "gzip download -> " + " success");


                    // read gzip 
                    ByteArrayInputStream bais = null;
                    try {
                        bais = new ByteArrayInputStream(responseBodyResponse.body().bytes());

                        GZIPInputStream gzis = new GZIPInputStream(bais);
                        InputStreamReader reader = new InputStreamReader(gzis);
                        BufferedReader in = new BufferedReader(reader);

                        String readed;
                        StringBuilder gzipResponseString = new StringBuilder();
                        while ((readed = in.readLine()) != null) {
                            gzipResponseString.append(readed); //write gzip data in StringBuilder
                        }

                        AppLogger.i(LOGGER, "Gzip string->" + gzipResponseString);


                    } catch (IOException e) {
                        AppLogger.e(LOGGER, "gzip extract exception->" + e.getLocalizedMessage(), e);
                    }
                }

                @Override
                public void onError(Throwable throwable) {
                    AppLogger.e(LOGGER, "gzip failed->" + throwable.getMessage(), throwable);
                }
            });

You can continue with DisposableSubscriber as per your requirement.

Aks4125
  • 4,522
  • 4
  • 32
  • 48
  • Hi, thanks for your answer but this one doesn't explain what's going on, So what you are doing here is getting the gzipped json file via retrofit and rxjava and unzipped completely or one by one for each string? what's getJsonByFormUrl() method? How do I know that I'm getting the data as form url encoded? Where should I handle the new requests for unzipped json? I have so many questions – Ege Kuzubasioglu Jan 10 '18 at 13:30