0

I am looking at sending objects over http from an android client to my server that is running java servlets. The object can hold a bitmap image, and I am just wondering if you could show me an example of sending an object from the client to the server.

I read on the forms that people say to use JSON , but it seems to me JSON works with only textual data. If it does not could someone show me how to use it with objects that contain images

4 Answers 4

2

To send binary data between a Java client and a Java server which is connected by HTTP, you have basically 2 options.

  1. Serialize it, i.e. let object implement Serializable, have an exact copy of the .class file on both sides and send it by ObjectInputStream and read it by ObjectInputStream. Advantage: ridiculously easy. Disadvantage: poor backwards compatibility (when you change the object to add a new field, you've to write extremely a lot of extra code and checks to ensure backwards compatibitility) and bad reusability (not reusable on other clients/servers than Java ones).

  2. Use HTTP multipart/form-data. Advandage: very compatible (a web standard) and very good reusability (server is reusable on other clients and client is reusable on other servers). Disadvantage: harder to implement (fortunately there are APIs and libraries for this). In Android you can use builtin HttpClient API to send it. In Servlet you can use Apache Commons FileUpload to parse it.

Sign up to request clarification or add additional context in comments.

1 Comment

Regarding backward compatibility of serialized objects: always defining serialVersionUID will help and it's good practice anyway. Also, avoiding the common pitfalls of serialization (eg. chapter 11 of Effective Java by J. Bloch) should keep you on the safer side.
1

I recommend you use XStream

XStream for your servlet side: http://x-stream.github.io/tutorial.html

XStream code optimized for Android: http://jars.de/java/android-xml-serialization-with-xstream

If you are sending images and such, wrap them into a 'envelope' class that contains a byte array like the one here: Serializing and De-Serializing android.graphics.Bitmap in Java

Then use HttpClient in your android app to send the data to your servlet ^^ Also make sure that both the app and the servlet have the same classes ^^

Comments

0

Socket Api is also well. Creating socket in both side will allow to send raw data to be transmitted from client android application to server.

1 Comment

I wouldn't go for something low level as Socket for a HTTP connection. 1 line of code is easier than 100 lines of code.
0

Here is the code for hitting a servlet and send data to the server.

boolean hitServlet(final Context context, final String data1,final String data2) {
    String serverUrl = SERVER_URL + "/YourSevletName";
    Map<String, String> params = new HashMap<String, String>();
    params.put("data1", data1);
    params.put("data2" data2)
    long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
    // As the server might be down, we will retry it a couple
    // times.
    for (int i = 1; i <= MAX_ATTEMPTS; i++) {
        try {
            post(serverUrl, params);
            return true;
        } catch (IOException e) {
            // Here we are simplifying and retrying on any error; in a real
            // application, it should retry only on unrecoverable errors
            // (like HTTP error code 503).
            Log.e(TAG, "Failed " + i, e);
            if (i == MAX_ATTEMPTS) {
                break;
            }
            try {
                Log.d(TAG, "Sleeping for " + backoff + " ms before retry");
                Thread.sleep(backoff);
            } catch (InterruptedException e1) {

                // Activity finished before we complete - exit.
                Log.d(TAG, "Thread interrupted: abort remaining retries!");
                Thread.currentThread().interrupt();
                return false;
            }
            // increase backoff exponentially
            backoff *= 2;
        }
    }

    return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.