views:

1002

answers:

2

I need to write an application that sends data to the Google App Engine from Android. The data that I would like to send can be described by a typical database record. Each record has some integers, strings, dates, etc. I would like to keep the connection details hidden/secured so that someone can't create false data in the datastore, if it it's not too involved.

My question is: what is a good way to get this data from Android devices into the GAE datastore? If you could post a link to the appropriate Android libraries, or a link to how this has been done in the past, that would be really helpful.

+5  A: 

Nothing Android-specific about this -- just send an HTTP POST to a GAE-served URL that will accept the request, process it, and store your data. There's no "Android-GAE" royal road -- a POST is just the normal way to perform this, whoever's sending it, whoever's serving it!

Alex Martelli
OK, but how would I keep that hidden, so that someone couldn't inject false data into my datastore? Aren't HTTP POSTs easy to see, and thus easy to recreate falsely? Sorry, I'm somewhat of a noob at this stuff.
Doughy
You should always assume that someone can eavesdrop on the data your program sends to the server and recreate false data. There's no way to send data that prevents this, because all of the information needed to make the connection is on the client, which you don't control.To avoid false data, you need to authenticate the user. Have them log in and get back a token.That still won't stop someone from uploading false data as a logged-in user. You have to handle that on the server side.
dmazzoni
+3  A: 

Yep, just send an http post. Here's a snippet of some code I used that worked fine on Android.

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

    try {
        int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams,
                                                  TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpClient client = new DefaultHttpClient(httpParams);

        HttpPost request = new HttpPost(serverUrl);
        request.setEntity(new ByteArrayEntity(
            postMessage.toString().getBytes("UTF8")));

        HttpResponse response = client.execute(request);
    } catch (Exception e) {
        ....
    }
dmazzoni