OkHttp返回不可读的字符 [英] OkHttp returning unreadable characters

查看:105
本文介绍了OkHttp返回不可读的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向服务器发送请求,并得到 Collection + Json 的响应.一切在PostMan中都是完美的.

I am sending a request to server and getting Collection + Json in responce. Every thing is perfect in PostMan.

但是当我使用OKHTTP在代码中执行相同的操作时,我得到了一些不可读的字符.

But when I am doing same things in code using OKHTTP, I am getting some unreadable characters.

这是我的代码

      OkHttpClient client = new OkHttpClient();

            requestBody = new FormBody.Builder()
                    .add("email", email)
                    .add("password", psd)
                    .build();

            Request request = new Request.Builder()
                    .url(url)
                    .addHeader("Accept", "application/vnd.collection+json")
                    .addHeader("Accept-Encoding", "gzip")
                    .addHeader("Authorization", "Basic YWRtaW46cmVhbHNlYw==")
                    .post(requestBody)
                    .build();

            try {
                Response response = client.newCall(request).execute();
              String s = response.body().string();

                response.body().close();
            } catch (Exception e) {
                e.printStackTrace();
            }

我尝试了其他一些网址,但这些网址运行正常.

I tried some other url and those are working perfect.

非常感谢.

推荐答案

最后我解决了这个问题,尽管我尝试了很多困难的方法:P

Finally I solved the issue and it wasn't very difficult one although I tried lot's of difficult approaches :P

我通过从代码中删除这一行来解决了这个问题

I solved the issue by removing this line from the code

 .addHeader("Accept-Encoding", "gzip")

希望它对像我这样被困的其他人有所帮助.

hope it help to some one other who got stuck like me.

谢谢

这篇关于OkHttp返回不可读的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆