Cancelling Aquery Ajax request



In our ongoing project we are heavily relying on network calls i.e. fetching data from the server and posting data to the server.Previously,we used AsyncTask and right now we found an awesome open source library named Aquery. This library is equipped with large set of functionality like image loading from server,using HTTP POST and GET,easy XML parsing with Aquery implemented XmlDom, easy object mapping to server response and many others. Moreover, this library is very easy to use and implement and kudos to all developer of Aquery for making such an useful and powerful class.
While using Aquery we needed to perform cancel to any server request via user button click action.But, first lets start with small Aquery demo

private Aquery aquery;
private String remoteUrl ="some remote url where you fetch some data";
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      aquery = new Aquery(this);
      //Aquery lets you define what type of class you are expecting as server request response like String,JSON,XML 
     //We are dealing with String class in this example
     //All the result is received via AjaxCallback class  callback() method,where you can do necessary operation
     aquery.ajax(remoteUrl,String.class,new AjaxCallback<String>(){
         @Override
        public void callback(String url, String result, AjaxStatus status) {
          //do your processing with server response
          processInformation(result);
        }
    });
}



My previous implementation on cancelling Aquery was outdated. So it wasn’t working anymore. Then a fellow developer named ariellopezf
had found the correct way to cancel Aquery of recent version. Though,I haven’t tested myself,I am completely relying on ariellopezf words and giving his solution of properly cancelling Aquery. See the comment’s of this post to know more about ariellopezf


 private AjaxCallback<String> ajaxCallback = new AjaxCallback<String>(){
    	@Override
    	public void callback(String url, String object, AjaxStatus status) {
    		   //do your processing with server response
                   processInformation(result);
    	};
    };
   //on our previous code
   query.ajax(remoteUrl,String.class,ajaxCallback);

   public void cancelAquery(){
      //this statement does cancel the request i.e. we won't receive any information on callback method
     //ajaxCallback.async(null);  
     
     ajaxCallback.abort();
}



This is all needed to cancel Aquery ajax request. And Aquery is very useful library to be used ,if app needs to communicate to server frequently and I urge every Android Developer to use it. I believe that someday Aquery will pave its usage to Android Framework
Till then enjoy coding,enjoy blogging 😉

Advertisements

6 thoughts on “Cancelling Aquery Ajax request

  1. good article. i am trying to upload file iwth aquery but i not able to do so .if you have any code uploading image and text with aquery to php script i shall be thankful

    • Hi sir i been stack with this problem for almost couple of days, i cannot cancel my pdf download request
      I already tried using this to cancel but its not working “AjaxCallback.cancel();”, ” aq.ajaxCancel();”
      and im using this code aq.progress(prgDialog).download(url, target,new AjaxCallback() { } to download the pdf,
      what i want is to cancel it when the phone call is interrupt and cancel it manualy

      thnk you so much, your the only last change of mine 😀

  2. Hi Ariel,
    I have lost track of usage of Aquery and while I was using it the following method did solve your issue. Could you try setting AjaxCallback object async to null like this AjaxCallback ab =new AjaxCallback() and set it on your aquery object . Then try ab.async(null) along with ajaxCancel. But I am not sure of it. If you have not heavily used Aquery to your project, then I advise you to switch to http://loopj.com/android-async-http/ which is easy to use as well and which has cancelling mechanism implemented.

    • Hi sir

      thank you so much for the quick reply, i already fixed my problem i can abort the pdf download when phone call is interupt of the user cancel it manually

      AjaxCallback ajaxCallback;
      boolean downloadCancel = false;

      prgDialog.setButton(DialogInterface.BUTTON_NEGATIVE, “Cancel”, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      cancelRequest();
      }
      });

      ajaxCallback = new AjaxCallback(){
      public void callback(String url, File file, AjaxStatus status) {
      if(downloadCancel){
      //message about download cancel
      // set downloadCancel to false again
      }else{
      //show some result about AjaxStatus
      }
      }
      };
      public void cancelRequest(){
      downloadCancel = true;
      ajaxCallback.abort();
      }

      BTW yout “Post Comment” button is disable i just just firebug to send this comment

      • That’s great. I think I should add your answer on my blog. Is it okay for you? I will mention you as well

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s