0

I'm using android-async-http 1.4.5, but I found it never calls my server, here's my code :

public class HttpUtils {
  private static AsyncHttpClient client = new AsyncHttpClient();

  public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    params.setUseJsonStreamer(true);
    client.get(url, params, responseHandler);
  }

  public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    params.setUseJsonStreamer(true);
    Log.i("URL", url);
    client.post(url, params, responseHandler);
  }
}

Here's how I define my handler:

public abstract class JsonResponseHandler extends AsyncHttpResponseHandler {
  private static final String TAG = "JSON_RESPONSE_HANDLER";


  public abstract void onSuccess(int statusCode, Header[] headers, JSONObject jsonObject);

  public abstract void onFailure(int statusCode, Header[] headers, JSONObject jsonObject);

  @Override
  public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
    Log.i(TAG, "on success");
    onSuccess(statusCode, headers, parseObject(responseBody));
  }

  @Override
  public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
    Log.i(TAG, "on failure");
    onFailure(statusCode, headers, parseObject(responseBody));
  }

  private JSONObject parseObject(byte[] response) {
    JSONObject jsonObj = null;
    try {
      String json = new String(response, "UTF-8");
      jsonObj = new JSONObject(json);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return jsonObj;
  }
}

After some debug work, I found my code actually execute client.post(...), but my server didn't receive any call from it.

EDIT

Here's how I use it in my test case :

public class VerifyCodeTest extends TestCase {

  private static final String TAG  ="VERIFY_CODE_TEST";

  public void testGetVerifyCode(){
    Log.i(TAG, "Hello World");

    VerifyCodeTransaction vc= new VerifyCodeTransaction("167xxxx137");
    vc.execute(new JsonResponseHandler() {

      @Override
      public void onSuccess(int statusCode, Header[] headers, JSONObject jsonObject) {
        Log.i(TAG, "SUCCESS");
        Log.i(TAG, jsonObject.toString());
      }

      @Override
      public void onFailure(int statusCode, Header[] headers, JSONObject jsonObject) {
        Log.i(TAG, "FAILURE");
        Log.i(TAG, jsonObject.toString());
      }
    });
  }
}
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
  • How can you tell your server didn't get the request? Also, have you tried to debug by using `curl` or `httpie` just to confirm the url / params are correct? – mbmc Sep 16 '14 at 16:28
  • @user2713030 , I'm sure the server didn't get the request because I'm developing the server at the same time(on another machine). The url is correct, I've tested it with `postman`. – WoooHaaaa Sep 16 '14 at 16:32
  • Do you know what type or object you're expecting in the response? I faced something similar in the past. I overrode `onSuccess(..., JSONObject response)`, and received nothing, but it worked with `onSuccess(..., JSONArray response)`. Same for `JSONObject` vs `String`. – mbmc Sep 16 '14 at 16:44
  • The problem is, `client.post()` doesn't send the request to server, so weird! Plus, I'm using it from a android test project. – WoooHaaaa Sep 16 '14 at 16:49
  • Is your client on device or emulstor? – greenapps Sep 16 '14 at 16:55
  • @greenapps, I'm using an `android 4.3` device. – WoooHaaaa Sep 16 '14 at 16:59
  • You tested 'it' with postman. But that was postman on another pc. Not on your device i think. Try that url in a browser on the device to see if you get a connection. – greenapps Sep 16 '14 at 17:02

1 Answers1

0

i don't know if you encounter this problem only in your TestCase or also in your App. I think the library doesn't work well for unit tests!! source:https://github.com/loopj/android-async-http/issues/321

i had a similar problem with the library in the past, i solved it by compiling the library from the source code in github repo

Maybe it's better that you use the latest version of the library :)

medhdj
  • 1,168
  • 10
  • 17