Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OkHttp returning unreadable characters

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

enter image description here

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

enter image description here

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.

like image 245
Syeda Zunaira Avatar asked Mar 03 '16 07:03

Syeda Zunaira


1 Answers

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

like image 155
Syeda Zunaira Avatar answered Sep 27 '22 16:09

Syeda Zunaira