0

I'm trying to access Google's geocode API using Android-Async-http library in my app. Here is my request and below is response:

���������������UMo�0��WX>Ӫ i{���UJ�$�J� mߔ%�===�������B��R��}���#�4�.�d�{��$�b��D��V<��<�����w*y�q���y5����o� �����

Here is success callback method of AsyncHttpResponseHandler.

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
    if (statusCode == HttpStatus.SC_OK) {
        try {
            String response = new String(responseBody);
            //String response = new String(responseBody, "UTF-8"); //this is also giving junk reponse
            Log.v("SUCCESS RESPONSE", response);
            networkCallback.onSuccess(response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

Why am i getting the response as junk? How do I get proper response?

Santhosh
  • 4,956
  • 12
  • 62
  • 90
  • What is the type of response? is it JSON or String?. May be your converting JSON Response as String. – Madhukar Hebbar Jan 04 '16 at 10:12
  • @MadhukarHebbar : Google's geocode API can return the response in json and xml formats. I have specified that in my request url which is json. – Santhosh Jan 04 '16 at 10:14
  • Yes Then you need to receive it as JSON right? like this `@Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { // If the response is JSONObject }` – Madhukar Hebbar Jan 04 '16 at 10:14
  • @MadhukarHebbar : There is no such that method in Android-async-http library. It just gives the http response in byte array format. So I'm converting byte array to string and then json object – Santhosh Jan 04 '16 at 10:18
  • What is the character encoding according to the HTTP header? – Bart Friederichs Jan 04 '16 at 10:22
  • Is perhaps gzip enabled? – Bart Friederichs Jan 04 '16 at 10:23
  • May be i am wrong but please check [here](http://stackoverflow.com/a/16790526/4596556) and [here](https://loopj.com/android-async-http/doc/com/loopj/android/http/ResponseHandlerInterface.html) – Madhukar Hebbar Jan 04 '16 at 10:24
  • @BartFriederichs : Yes. It is enabled. Do I need to change it? If so, How do I change it? – Santhosh Jan 04 '16 at 11:05
  • You can change it in your request headers, or enable gzip in your receiving side. Refer to the library's documentation for that. It says it supports automatic gzip, so I reckon you have some mistakes in your request. – Bart Friederichs Jan 04 '16 at 11:33
  • Another solution might be to manually unzip the data you get. – Bart Friederichs Jan 04 '16 at 11:34

2 Answers2

0

Try this :

   @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            MyLog.log(TAG, response.toString());
            // if is Jsonobjec     
            }

 @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
            super.onSuccess(statusCode, headers, response);
             MyLog.log(TAG, response.toString());
            // if is JsonArray    
        }

Or you want get response string:

 @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            super.onSuccess(statusCode, headers, responseString);
        }
Quang Doan
  • 632
  • 4
  • 12
0

Used

 compile 'com.loopj.android:android-async-http:1.4.9' 

instead of 1.4.5

Santhosh
  • 4,956
  • 12
  • 62
  • 90