3

I am using Android loopj Async Task for accessing data from server.

I am trying to implement this on an autocompleteText view , ie when user types it will search in my database and listout the result in a ListView using adapter. All are working fine.

But I need to cancel all previous requests when a new task run. ie if user type "che" then new AsyncHttpClient run and start web service, then user type "chem" then I need to cancel all previous / running AsyncHttpClients. I

Any idea ?

Here is my code

public void getValues(String movie){
AsyncHttpClient client = new AsyncHttpClient();
RequestParams webparams = new RequestParams();
webparams.put("fn", "searchMovies"); 
webparams.put("movie", movie);


client.post(domain, webparams, new AsyncHttpResponseHandler() {

    @Override
    public void onStart() {

    }

    @Override
    public void onSuccess(String response) {
        //dialog.dismiss();

        try {

            JSONObject obj = new JSONObject(response);
            ...
            ...
             }

            catch {}


    @Override
    public void onFailure(Throwable e, String response) {
        Toast.makeText(SearchActivity.this,"Error Occured ! Please try again.",Toast.LENGTH_SHORT).show();
        cd.goHome(SearchActivity.this);
    }
    });

}
ramesh
  • 4,008
  • 13
  • 72
  • 117

1 Answers1

6

Please look at this method of the AsyncHttpClient class method: cancelRequest().

You can cancel any pending (and interrupt if running) request associated with the Context. It means you have to pass a Context in your requests, otherwise they will be not cancelled.

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
fasteque
  • 4,309
  • 8
  • 38
  • 50
  • Thanks for your answer. Where I need to put this handle request ? In OnStart ? – ramesh Feb 15 '14 at 10:31
  • It depends on your app design and where you want to cancel the request. It could be when you enter or even better when you leave a screen, so in the onStop method of your Activity. Or maybe any time you invalidate a search operation, before starting the new request, you could cancel any previous one. A simple client.cancelRequest(...). If you didn't already do that, I suggest to manage the library as a static HTTP client as explained in the home page: http://loopj.com/android-async-http/. – fasteque Feb 15 '14 at 10:35
  • So I need to initialize the client on onCreate method as static and do all other activities right ? – ramesh Feb 15 '14 at 10:48
  • As in the example, create a separate class in your project. As you see the client is created inside and is private. Then you have public methods wrapping the client features (get, post, ...). In your code simply use these methods directly. Like MyClient.get(...) where MyClient is the name of your class (in the example is Twitter...). – fasteque Feb 15 '14 at 11:04
  • What does a request associated with the Context mean? Would you like to explain that? – DYS Jun 05 '15 at 03:27
  • @DannySuen you can refer to this to have a better understanding of a Context: http://stackoverflow.com/questions/3572463/what-is-context-in-android. And of course the official documentation: http://developer.android.com/reference/android/content/Context.html. So when you perform a request, if you pass also a Context (Application Context or simply an Activity Context) you will be able later to cancel that pending requests using the API cited in my answer. – fasteque Jun 05 '15 at 06:22
  • @JalpeshKhakhi you should provide more info so we can try to help you. If your issue is different from this one, please ask a new question. – fasteque Feb 08 '17 at 10:02
  • I am just sending Image to server using Base64 string in request, and have also provided Cancel button to user so if user press cancel button before request being completed, that WS call should be cancel. – Jalpesh Khakhi Feb 08 '17 at 10:43