0

I'm developing a client-server android application, which makes use of REST protocol.

Basically I upload a picture to the server first and then process it on a server and then update the status of this picture in android application (from new to processed);

so the user needs to press "refresh" to receive the renewed list of processed pictures from the server...

I'm surely don't like it...

is it possible to have some sort of callback after the server has completed processing? I think i'm looking for somewhat like... ajax, in other words, the server needs to trigger the event of updating the list, not users "refresh" button pressing. I also don't wan't to ask server every ~2 seconds after sending the picture if it's been processed or not ... i think there must be more elegant solution.

What is the correct way to implement such a functionality?

I can change both server and android parts of an application

thank you in advance!

tania
  • 1,086
  • 1
  • 12
  • 31
  • Why can't you just send a message from server to client? – Semyon Danilov Aug 08 '13 at 13:54
  • yeh, my question is...how do i do that? – tania Aug 08 '13 at 14:44
  • Use this answer (http://stackoverflow.com/questions/3696986/how-can-i-send-an-http-response-using-only-standard-network-libraries) to learn how to send responses for your requests (if you don't know). And then simply create and send response like "PROCESSING", "DONE" etc – Semyon Danilov Aug 08 '13 at 15:13

2 Answers2

0

This is what Google Cloud Message is for, the server notifies a device or devices that there is new stuff to get from the server. the phone makes the call to the server to get all the new information and process it

Another option is using xmpp, take a look at asmack

tyczj
  • 69,495
  • 52
  • 182
  • 280
0

You can use droidQuery to perform an Ajax request with similar syntax to Javascript. It is highly configurable, so check the javadocs for help (if you have used Javascript, you will catch on really fast). The call will run in a background thread, but the callback methods will be called on the main thread. Here is an example of how to do a post that gets a JSON response:

$.ajax(new AjaxOptions().url("http://www.example.com")
                        .type("post")
                        .dataType("json")
                        .data(bitmap_bytes)
                        .context(this)
                        .success(new Function() {
                            @Override
                            public void invoke($ droidQuery, Object... params) {
                                JSONObject json = (JSONObject) params[0];
                                //handle json response. Also see $.map(JSONObject) for parsing
                            }
                        })
                        .error(new Function() {
                            @Override
                            public void invoke($ droidQuery, Object... params) {
                                AjaxError e = (AjaxError) params[0];
                                Log.e("$", "Error: " + e.status + ": " + e.error);
                            }
                        }));

If you want to add a progress spinner, droidProgress is a droidQuery Extension, and can very easily be integrated with droidQuery.

Also, you can see how to convert a Bitmap to a byte[] at converting Java bitmap to byte array.

Community
  • 1
  • 1
Phil
  • 35,089
  • 23
  • 123
  • 159