Home » Android » How to send data from Android mobile devices to the Google App Engine datastore?

How to send data from Android mobile devices to the Google App Engine datastore?

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

How to&Answers:

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) {
        ....
    }

Answer:

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!