AsyncHttpRequest POST not triggering callbacks (android-async-http by loopj)

ぃ、小莉子 提交于 2019-12-21 04:40:55

问题


I'm trying to send a POST to a server using loopj's async http library. The following code is pretty standard but I can't get it to work.

I have debugged quite a bit and have checked the following:

  • The URL is correct.
  • The params are correct and they are stored correctly into the params variable.

It appears that when HttpResponse response = client.execute(request, context); is called in AsyncHttpRequest.makeRequest() the request doesn't contain any params:

EDIT : After looking at the code a little more it seems the POST params might be included in the entity or headergroup.

But the real problem is that I don't get any of the callbacks (see code below). The if statement after the client.execute() call never gets called (the breakpoint isn't triggered).

RequestParams params = new RequestParams();
params.put("loginid", "user");
params.put("password", "pass");
params.put("deviceid", "deviceid");

AsyncHttpClient client = new AsyncHttpClient();
client.post(MDSettings.BASE_URL + "/user/login", params, new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response) {
        Log.d(MDSettings.TAG, "MDConnectionManager - login - response: " + response);
        singleton.connectionSuccessful(response, returnIndex);
    }

    @Override
    public void onFailure(Throwable error, String content) {
        Log.d(MDSettings.TAG, "MDConnectionManager - login - content: " + content);
        singleton.connectionFailed("Login Failed", error.getMessage(), returnIndex);
    }
});

I'm using this lib because it lets me send files which I'll need later into this project. If there is another lib that does the same thing (and actually works) that'd be fine with me.

My question is: Why don't I get any callbacks and how can I fix this? Tell me when something isn't clear and I'll try to explain in more depth.


回答1:


It seems my android.permission.INTERNET entry was removed from the manifest file. After adding it again everything worked as expected. Every time something doesn't work I'll be checking my permissions.




回答2:


Try this simple snippet.

This works on my device.

It prints "Failure" as POST is not allowed (HTTP status: 405)

AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("q", "google");

client.post("http://google.com", params, new AsyncHttpResponseHandler(){
     public void onSuccess(String arg0) {
         Log.d("TAG", "Success");        
     };                                      

     public void onFailure(Throwable arg0, String arg1) {
         Log.d("TAG", "Failure");        
     }; 
}); 



回答3:


Well I know nothing about this library, but why not try to make something similar to it, using AsyncTasks? For example you can subclass AsyncTask and provide it URL which to load. Another way is to subclass AsyncTask and provide it a Callable object. This approach will allow you to generalize this class making it suitable to perform different long-time tasks, not only network requests. Both methods can utilize some interface with callbacks (onSuccess, onFailure). And in onPostExecuteMethod you can determine whether there was an error during task execution or not and provide neede callback.




回答4:


here in your code you directly calling the responsehanlder from post method. first:

 client.post(MDSettings.BASE_URL + "/user/login", params, responseHandler);

in the next line just add get method

 client.get(MDSettings.BASE_URL + "/user/login", new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            System.out.println(response);
        }
    });

then u will get response from it



来源:https://stackoverflow.com/questions/17473853/asynchttprequest-post-not-triggering-callbacks-android-async-http-by-loopj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!