1

I used LoopJ AndroidAsyncHttp to get the response from the url, but the code didn't go into onSuccess() or onFailure(). The code is as below:

public void queryTopic(RequestParams params) {
    AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://192.168.0.109:8080/PhoneServer/topic/query", params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            System.out.println("It's in onSuccess");
        }

        // When the response returned by REST has Http response code
        // other than '200'
        @Override
        public void onFailure(int statusCode, Throwable error,
                String content) {
            System.out.println("It's in onFailure");
        }
    });
    System.out.println("It's over");
}

It just printed out the "It's over". What's the matter with the AsyncHttpClient?

Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29
Eric Cheng
  • 21
  • 5

3 Answers3

0

How are you calling this? Is the context alive? If you are calling it from some place where context is no more available, then you will never get this callbacks.

I think you should try calling this queryTopic() method on click of some button and then wait for some time, you should get the response.

  • I called this queryTopic() in a onCreateView of a fragment, is it ok? – Eric Cheng Sep 21 '15 at 05:32
  • During onCreateView, activity is not created. I think you should call it once activity is created. Try calling it once activity is created maybe from onActivityCreated(). – Digish Gabhawala Sep 23 '15 at 05:01
0

maybe the problem is that you ask to the server for a String

public void onSuccess(String response){...}

but server answer with a JSONObject

You can try this code:

AsyncHttpClient client = new AsyncHttpClient();
            client.setTimeout(5000);
            client.get(yourActivity.class, yourLink, new JsonHttpResponseHandler(){

            @Override
            public void onStart() {
                    Log.e(TAG, "start");
            }

            @Override
            public void onSuccess(int status, Header[] headers, JSONObject answer) {
                    Log.e(TAG, "SUCCESS");
                    Log.e(TAG, "print => "+answer.getString("answer_id")); //"answer_id" is a random example

            }

            @Override
            public void onFailure(int status, Header[] headers, String answer, Throwable throwable) {
                    Log.e(TAG, "FAILURE");
            }


    });
Jotunheim
  • 110
  • 1
  • 11
0

onSuccess() is an overloaded method be careful regarding what you send from server, if it is a jsonObject or jsonArray or simple string. Use corresponding overloaded method of onSuccess(). I am using it too for a jsonObject response and i confront no error or irregularities in the method.

I return jsonObject from server for which my code works as expected and is as follows:

        RequestParams params = new RequestParams();
        params.put("pet","Cat");
        params.put("name","Maran");
        RestClient.get("/savelocation", params, new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Toast.makeText(context,response.toString(),Toast.LENGTH_SHORT).show();
                Log.e("Error makerequest","request completed");
            }

            @Override
            public void onFinish() {
                //onLoginSuccess();
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable,JSONObject errorResponse){
                Toast.makeText(context,throwable.toString(),Toast.LENGTH_LONG).show();
            }
        });

Note: RestClient is a static instance of AsyncHttpClient

Anurag Kumar
  • 1,355
  • 11
  • 11