I am making a chat application.The server will send me an update whenever a friend sends me a message.As a result i need a continuous listener from my android client.I have used asynchronous task and in the post execute method, i have called the asynchronous task again.Thus listening continuously.But this is giving me weird errors.
If you could help me i would be really grateful.If you think i should implement the continuous listener in some other way please do suggest me.Thanks.
Proper method should be like when you get connected to server, you should keep listening in a loop. See my answer here
and modify it as needed.
What about using http://quickblox.com/developers/Android library? I am just curious, if it would not be easier :) .
Otherwise I thought about making while cycle in doInBackground() method, instead of calling asyncTask all over again.
Something like -
While(programExit) {
//your code
}
Related
Im learning from Android's BluetoothChat sample app and noticed that they are using a Handler to send updates to the UI as seen here . I was wondering why they wouldn't prefer to use a callback/listener to send updates to UI?
My guess is because Handler posts messages on the Thread it was created on. If you were using callbacks, you would have to take care of calling runOnUiThread(Runnable) in order to perform any kind of UI changes.
For a Guess.
Handler is a perfect async solution. just post message to the MessageQueue,Handler(UI Thread) will take message from it. It can reduce your module's complexity.
CallBacks is another solution,but complex according Handler to update UI.
For my game, I have it running on two servers (one for the game, one for the login system). They both need to interact with each other, and sometimes, ask questions about the state of something else in the other server.
For this example, the game server will be asking the login server if a player is trying to log in:
public boolean isLoggingIn(int accountId) {
//Form a packet to send.
int retVal = sendData();
return retVal > 0;
}
Obviously I'd use an int so information other than booleans can be returned.
My question is, how do I get this modal-style programming working? It'd work just like JFileChooser's getOpenDialog() function.
Also, I should mention that more than one thread can call this method at once.
I assume by modal, you mean trying to block all actions except one. I strongly suspect that this style will lead you into trouble. Modal interaction is a form of locking and therefore not very tolerant to hangups and disconnects and such. To make it tolerant, you need timeouts and cleanup code for cases when someone entered a mode and then nothing further happened. (i.e they closed their laptop, or the game crashed, they unplugged the network cable etc).
If I were you I would instead try to think of things in terms of authentication and authorization.
The quick answer - you need to expose methods on both servers as RMI-capable, and simply invoke methods like you described.
You might find it useful to review the official Oracle RMI tutorial: http://docs.oracle.com/javase/tutorial/rmi/index.html
Althought your design might be wrong - it's your design, and why not letting you shoot your head? ;)
Also, it's worth looking at Spring Security: http://static.springsource.org/spring-security/site/
If you use something like this on a thread that is supposed to handle other requests after it, it would hang up all those requests while it is blocking for a return value if the latency between the game and login servers is high. Certainly what you want instead is a callback so that your thread could handle other requests while it waits for a response.
I see no reason to halt execution of a thread until a value is received. If you need the value for an operation after it, then just copy all the code you have after the call you want to be "modal" in the callback. If you expect to send multiple requests while still waiting for a response, then send a unique "responseId" from the requester's side that the responder can include in its response. Use the "responseId" as a key for a Map with Runnables as values. When you receive a response, call remove on the Map with the responseId key and call run() on the Runnable value that is returned. MINA is supposed to asynchronous and should not block for a response packet.
If you have a really good reason for why you want to handle it all on the same thread, you can look into the java.util.concurrent package. I would implement it using a CountDownLatch of count 1, call await() after sending a request message, and call countDown() when you receive a response by MINA. You have to use an AtomicReference or an array of length 1 to hold the value you received in the response that you can read back into the waiting thread.
PS, you still doing MapleStory work?
I'm facing this problem: after clicking on a button, I make a request to the server and get some data; then, I display the data on a new page/view. This raises a problem: the UI has to wait while the request is being made and data is being received, parsed and set on the view. This results in the user having to wait until all the data is loaded before even being able to go back, and doesn't even have the chance to cancel the call. Multithreading would solve the issue, and that's where I need help.
The HTML5 Web Workers would do the trick for me, however I don't want to "hard code" them in JSNI and have all the calls written with Javascript instead of GWT Java (RequestBuilder). I've read about DeferredCommand but I also don't think it's the answer to my issue.
Any suggestions? Or this is an impossible optimization, at the moment?
In JS, therefore GWT, there is no multithreading. Instead you should use asynchronous calls with callbacks. Normally when you use GWT RPC for communication, you issue a request and handle result in onSuccess event. Alternatively you can always use Timer to check for result periodically. I'm not sure what kind of request you are making, so hard to be specific. Probably you should check appropriate section of Communicating with the server
EDIT: I've just noticed you mention RequestBuilder. The sendRequest() should not block execution and you should process result in RequestCallback.onResponseReceived() of provided callback. Which mean you somehow continue your button event handling in that callback.
I have a base abstract class that all my asynctasks extend from. I have built in error handling for network errors where I display a dialog to the user telling them they can retry their request (after all it was a network error...).
I am having a bit of a problem "retrying" my asynctask since once it is executed, you cannot execute it again. How could I go by do that ? Should I use reflection to instantiate the same class and retry?
Has anyone dealt with this problem before?
here seems to be the answer you need:
How to run a retry when download fails in AsyncTask
AsyncTasks are single use and can only be executed once. An exception will be thrown if a second execution is attempted. The solution is just create a new AsyncTask and execute it.
I managed to programm an app that communicates with a server. I can write and I can read. I even managed to do it with Java NIO.
My problem is that I have a endless while loop that is listening for new data to read. It blocks the whole program and I can't write anymore data.
I need a solution so the loop keeps running in background listening for new data to read while I send data.
Any suggestions?
Use AsyncTask. It was created just for this kind of jobs (doing long running background tasks, while still have a possibility to update UI).
You could either create a separate thread to handle the socket asynchronously and pass messages through a handler, or create a local service to handle the comms.
You should never do a long wait on the main (UI) thread.
If your loop is blocking the whole program there is something wrong with it. I don't see what other answer you can realistically expect until you post some code.