简体   繁体   中英

Retrofit - Decompress gzip

I need to implement for one of my network requests a way to decompress gzip then decode base 64 and then convert the bytes to String. I am using retrofit and have implemented as shown below.

I have tried to implement it myself using this answer ( okhttp 3: how to decompress gzip/deflate response manually using Java/Android ) but to no success.

I have also tried to leave it blank because apparently, okhttp does this manually but whatever I do I get the results shown below.

Implementation of retrofit

                .connectTimeout(connectTimeout, TimeUnit.SECONDS)
                .writeTimeout(writeTimeout, TimeUnit.MINUTES)
                .readTimeout(readTimeout, TimeUnit.MINUTES)
                .build();

        if (User.getInstance().isDevMode()) {
            retrofit = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(App.getContext().getResources().getString(R.string.dev_server))
                    .addConverterFactory(ScalarsConverterFactory.create()).build();
        }

The expected results once I have decompressed the data and decoded should be a string for example "Failed: Bad internet" (much longer normally).

The actual results are for example: m ] k{ ȵ. W yNw lJA᎜/G [˶bɭN& HED b >k "APm' { [ Q ]W ί q! y _ ^, ' Cz+b dg G? $ y t T <aI: - / g6 d<e HYr Op< fi U m ? ?,eƷ / _ iG 7 y E ןZG/? i] \\ > _ .>\\ _ <̋) h ہg | ķ G 1 m~x* , ~<Y!"} o e _9Γ f2 l 0 Wy*e__ O ){' ; _ I 1 a ^֥ " 0_ Xx ) z8N 8O Oq ~ a o. .7 'o x ,g e Å H K ՛ E <ϗ _ e L b, D, Ǔd ϴ B+ \\0 _ oxF [ i * R1f? W }U Y pt* $ ,/ ٶ x d.iI^] Ǻ g! _ ҤX ?/E 2 a $?ø<-$ rO i p L y

I came across the same issue.

Remove: "Accept-Encoding: gzip, deflate" from the headers.

Keep: "Content-Type: application/json;charset=utf-8", "Accept: application/json"

When you provide your own Accept-Encoding header you're instructing OkHttp that you want to do your own decompression. By omitting it, OkHttp will take care of both adding the header and the decompression.

As answered here by Jesse Wilson: Retrofit and OkHttp gzip decode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM