7

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.

4
  • Check and see if its properly implemented
    – Junaid
    Commented Mar 3, 2016 at 7:46
  • Well this is for the Accept part.
    – Junaid
    Commented Mar 3, 2016 at 7:49
  • Check
    – Junaid
    Commented Mar 3, 2016 at 7:57
  • okay I tried by adding this header too .addHeader("Content-type", "application/json") . and again no luck. Commented Mar 3, 2016 at 7:58

2 Answers 2

12

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

0
1

Had a similar issue while fetching available languages from a web API. Adding UTF-8 encoding as header solved it for me:

.addHeader("Accept-Encoding", "UTF-8")

Maybe this helps someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.