Search code examples
javaandroidgoogle-app-enginehttpdatastore

Using POST to send data from Android to AppEngine Datastore


Sorry if this is a simple question, but I just can't figure out what I'm supposed to do and I think I'm a bit out of my depth here. I want to send data from an Android Application to my application running on Google App Engine. From there the data must be written to the Datastore. My data will mostly be in the form of objects, and I am working in Java.

I came across the following question: how-can-i-connect-a-google-app-engine-application-with-my-android

I think I understand what has to happen on the Android side. I also understand how the doPost method works and what it's supposed to do. But my problem is this, how can I now use PersistenceManagerFactory to write this data to my Datastore, or retrieve it? I.e., how would the code inside the doPost look, if I wanted to use PersistenceManagerFactory there?

Essentially, my mobile application must be able to do the same as my AppEngine application w.r.t. data retrieval/writing.

Or should I forget about this method and do something different?

Any pointer in the right direction will be greatly appreciated!

Edit:

I have started implementing Sam's solution as shown below. However, something is still nor right. here is what I am currently doing:

Mapping my servlet in web.xml of my GAE-app:

<servlet>
    <servlet-name>this_servlet</servlet-name>
<servlet-class>*app-details*.server.JsonNews</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>this_servlet</servlet-name>
    <url-pattern>/my_app/my_app_place</url-pattern>
</servlet-mapping>

The implementation of the doPost() Method is the same as Sam's.

In my Android-app:

DefaultHttpClient hc=new DefaultHttpClient();
ResponseHandler <String> res=new BasicResponseHandler();
HttpPost postMethod=new HttpPost("http://my_application.appspot.com/my_app/my_app_place");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
nameValuePairs.add(new BasicNameValuePair("value1", "Value my user entered"));  
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
String response=hc.execute(postMethod,res);

However, the hc.execute method fails, and provides me with the following message: Internal Server Error

This leads me to believe that the error must be on the server side- but I am not sure where or why things are going wrong. I copied Sam's code exactly, only changing the necessary things like "News".


Solution

  • This question solves my exact problem:

    how-to-send-a-json-object-over-request-with-android

    See dmazzoni and primpop's answers.