OkHttp returning unreadable characters

瘦欲@ 提交于 2019-12-12 10:49:23

问题


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

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

Here is my code

      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.

many thanks.


回答1:


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.

Thanks



来源:https://stackoverflow.com/questions/35765901/okhttp-returning-unreadable-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!