• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to implement a callback method for a Servlet Post Async task?

 
Cris Benois
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I am developing an app with a Java Servlet backend, and I am trying to get the Async call (AsyncTask - Android Developers) to get my response message Synced with the rest of the client and being able use this information wherever I want. Without a callback method, when from the caller class I use the commands:


I receive a NullPointerException pointing to the third line Tours ttours = s.tours;, since the s.execute() method doesn't wait for the rest of the lines to get executed.

To solve this I thought about implementing a callback method with interfaces in Java, but I am not sure on how to do it. Could you help me out?
For example, what class does have to implement the interface, the ServletPostAsyncTask or the caller class? thanks
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't there some kind of status method (say getStatus() on ServletPostAsyncTask s) which you can use to check if the execution is complete?
 
Cris Benois
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there is!! in fact the command s.getStatus().toString is always returning "RUNNING".
How can I sync the whole message exchange with the client?
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just checked the different statuses here - http://developer.android.com/reference/android/os/AsyncTask.Status.html
There are some suggestions given in the link on other ways to achieve executing the task instead of execute() method. Did you try using them?
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, here is a quick program i wrote to check this;

This is from my home page activity with itemclicklistener. So on click the servlet task code should run.



and here is my ServletPostAsyncTask (I used the same name)



here is the order of my log...



and the task completed with the expected result.

Are you doing something like this?
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh!! I got your problem. My code worked because of the break; (My bad!!!)
Give me sometime, let me check.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The status flag is marked volatile in AsyncTask and never gets to finished status as far as I ran the code. However I assume the operation you want to do once the status is finished ideally can be achieved in this method
"onPostExecute(String result){ //your FINISHED status code goes here... }" inside AsyncTask. This onPostExecute Method is expected to run after the "doInBackground" method inside AsyncTask.

The "doInBackground" method can actually do the background job you want to do like loading of a image, reading a file, copying a file etc., The "doInBackground" method is ran at some point after your "execute" method is called.

execute() ---> doInBackground() ----once complete----> onPostExecute().
 
Cris Benois
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all thank you for your answers. Sorry for the delay, but I think that we propally live in two different time zones .
All right, so what I am doing in onPostExecute is to deserialize a Json string (created with Gson) received by the Servlet, this way:


The Toast message is just to check on the returning message for debugging purposes and its execution applies indipendently from the rest of the code (like an indipendent popup message).
The problem is that the global field is what I need from the caller's class in order to proceed, as I am dinamically updating the app's UI depending on the content of the Tours object, so I really need to wait for it to be ready before proceeding so to avoid a NullPointerException.

My caller class works this way (I use code from this tutorial Android series: custom listview items and adapters):



..but as you outlined I also get stuck within the while and the Async task never gets finished...
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if this link helps - Passing Activity reference
Basically, you need to pass the reference of the activity to the AsyncTask.
 
Cris Benois
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! I think I solved it, as explained in this thread:

Access Servlet AsyncPost task response from fellow classes

Do you think it is a good solution, or I can do better?

Thank you!
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think both the links do the same thing. Passing the reference of the activity to your asyncTask.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!