我正在尝试从API调用中读取原始响应。
我正在使用:
com.amitshekhar.android:android-networking:1.0.0
建立在okhttp上。
所以我想记录API调用的原始响应。
据我所知, response.body()。string(); 只能调用一次。它已经被图书馆调用了一些点,所以我不能用它。
所以我使用了:
ResponseBody responseBodyCopy = response.peekBody(Long.MAX_VALUE);
String rawResp = responseBodyCopy.string();
但是当我使用它时,我得到了这样的原始响应:
12-11 09:41:30.564:I / RealInterceptor(5416):URL:https://myurl.hu/api/mobile/api.php?method=something
12-11 09:41:30.564: I/RealInterceptor(5416): Param: param1=12345
12-11 09:41:30.564: I/RealInterceptor(5416): Param: param2=abcdef
12-11 09:41:30.564: I/RealInterceptor(5416): Header: Content-Type: application/x-www-form-urlencoded
12-11 09:41:30.564: I/RealInterceptor(5416): Header: Content-Length: 40
12-11 09:41:30.564: I/RealInterceptor(5416): Header: Host: myurl.hu
12-11 09:41:30.564: I/RealInterceptor(5416): Header: Connection: Keep-Alive
12-11 09:41:30.564: I/RealInterceptor(5416): Header: Accept-Encoding: gzip
12-11 09:41:30.564: I/RealInterceptor(5416): Header: User-Agent: okhttp/3.6.0
12-11 09:41:30.564: I/RealInterceptor(5416): HTTP Status code: 200
12-11 09:41:30.564: I/RealInterceptor(5416): Raw response: ??????????????\ks??+?>[GD?n?i3?u:Izs??@$(?? ?(????C???Mcy<cr?H????aC ??fyV??QF????fFq???@??‘d??????$N?`4??Z??
????YN?????!>3"?e?5b?@#AkNRP|~?????????)I??+??$?]I?w%!?J?"Y+})-?? ?7[???@??F??e?r?.?L?#?0?rg?T???f?8ohGq?:???B????tGr-A/$ ?I?/Z??V???g@d?l@4?^??8?I???9??r?|??w?=??<QiH;2~?t?/??q???iDQ???VV?'x_&6\???|E???G??3~b??E???aG(u??B?b@7[????????w ?`x???!6*E?g???H??????r?8??)B?%,??+??&?]??"?)?K?a?Sk????T???y??r?'B ???H???fQ"ow2JB?,??1????8:??{?????(o:??H??f?FN??x$?k??_Y??+K?.k?n?H:??}\?lp?tj?2??\????h?8h???5? ????%??1N?i:????q?? ???0f??o????????_3vK3^d??|k1?0??u?"/????????E?X??<?k?Cin??"?u??8? N2_?*$E?
12-11 09:41:30.564: I/RealInterceptor(5416): ???b?
12-11 09:41:30.564: I/RealInterceptor(5416): ?? C?t?Q_??t?L???2?<.? ?!{???'??N?i??x1????h?O?g???L?<?H??????
12-11 09:41:30.564: I/RealInterceptor(5416): ???1??o#??c
12-11 09:41:30.564: I/RealInterceptor(5416): ?????.?????n_?*9n???<?? ?I{??;"F??J??M?x???8??7B;Ez??\TI??z??$o70????KHm??D0s@a???2C????ZNjq??????RS??f?E?$4U?e?(r???"z?6-?:??!?8?S??$,? ????cQ?~D??WXEE*?*8;????6eyB?q*y-O??o???????????s?%iK {??a?3`??]??h??????n ##?
W???(?di7????<?7q.b?l??C?;?0q=?H??/????z??)??R_?$ k??:????H??:?e?E????8?a?[?YD?E??}9??s<?? ??3N<??lr}?? x? ?ta??f?e?c??)y'?R???B9
12-11 09:41:30.564: I/RealInterceptor(5416): y3??WU??f%x?<!?Ic?2n??@#?l???4? ?$M^V?$?cV??1?v
?k?(??)?L.O(]??pt?*]????3??????l?q??<?_;~????jOG??^??B???#? ?!?K? ??+??VU?cIx????n L|?-0#????I?y??y?@'E*?$?????uy??+[?@???0?U64#?v_??(`o???q?8e??\O??n??O???R???k??w*I?%@_?s?I?a?5???5?Y,2?{_U?!?
???Eb3?|^ x??D[^????V??O?????r????j???????s???????C?????U+???}?Z=?^?v%?<?K2????@ă?p?V?"??Eq?Q?+?6?U?????e26???? ??@??U;A?S???E?riek?dX[?w???H\????8%?,%?yšp????g{??ap????g??|!@;?a@?2???QlK(V$X/?#????>do???Zcbwb?9??i?*RUP??S??M??^?UH?{2? S??M?.?c??x???`??Ŕ???=??Ae?AE?u?=???&??i??????6?+C7d???~Q?+p;*[??#??F??U?jV??????e?jZ????u?k??-Q"m]?`N?????uw?Z#M?2??o$??H?C0?dfNN4?????d????aX??C-T???"o????????Y?C?????????Lx\??l?$?Ob=?/?,????g???*??3??d?c
那是什么?
任何想法或帮助我应该怎么做我应该如何编码这个或人类可读的东西?
我曾尝试将其编码为UTF8但事实并非如此:)
编辑:
我试图将其解压缩为@Selvin告诉我:
public static String decompress(final byte[] compressed) throws IOException {
final StringBuilder outStr = new StringBuilder();
if ((compressed == null) || (compressed.length == 0)) {
return "";
}
if (isCompressed(compressed) ) {
final GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
outStr.append(line);
}
} else {
outStr.append(compressed);
}
return outStr.toString();
}
public static boolean isCompressed(final byte[] compressed) {
return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
}
现在它只返回:
12-11 10:08:33.155: I/RealInterceptor(6289): Raw response: [B@59864f3