2

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".

Community
  • 1
  • 1
hjweide
  • 11,893
  • 9
  • 45
  • 49

3 Answers3

1

You should use RequestFactorys to solve this problem.

https://developers.google.com/web-toolkit/doc/latest/DevGuideRequestFactory

With RequestFactory you can use a proxy object on the client (andorid or gwt-client) and send it back to the server.

Check this example: http://code.google.com/p/cloud-tasks-io/source/browse/trunk/CloudTasks-AppEngine/

Here is an Andorid app which registers via RequestFactory on C2dm (Push Service)

Sam
  • 2,707
  • 1
  • 23
  • 32
  • Thanks, I also came across this while searching, but it seems a lot more complex than what I need to do. I would prefer to just use doPost(), as I do not have a lot of time to implement this solution. However, if there is no simpler way I am prepared to use this. – hjweide May 07 '12 at 17:14
1

You can also make a simple HttpServlet. Following example returns a JSON String of all News items in datastore...

public class JsonNews extends HttpServlet {

@Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

        PrintWriter out = resp.getWriter();
        resp.setContentType("application/json");

        //out.write("{news}");

        PersistenceManager pm = Pmf.getPersistenceManager();
        Query q = pm.newQuery(News.class);

        List<News> news = null;
        String retStr="";
        try {
            news = (List<News>) q.execute();
            Gson g = new Gson();
            //retStr = g.toJson(news);
            retStr = "{\"success\": true, \"news\": " + new Gson().toJson(news,new TypeToken<List<News>>() {}.getType()) + '}';

             out.write( retStr);
        } catch (JDOException e) {
            e.printStackTrace();

        } finally {
            q.closeAll();
            pm.close();

        }

  }
}

Singleton Pmf-Class

public final class Pmf  {
private static final PersistenceManagerFactory pmfInstance =
    JDOHelper.getPersistenceManagerFactory("transactions-optional");

private Pmf() {}


public static PersistenceManager getPersistenceManager() {

    PersistenceManager pm = pmfInstance.getPersistenceManager();

    return pm;


}



}
Sam
  • 2,707
  • 1
  • 23
  • 32
  • Hi, sorry for taking so long to get back to this. I have edited my question to show how I have started implementing your answer, but I have encountered a problem. I don't think it's a major issue, probably just my lack of experience. I would appreciate if you could give it a quick glance when you have a moment. Thanks for all your help so far! – hjweide May 09 '12 at 19:53
  • I managed to get it working. Thanks for your help though, you put me on the right track! – hjweide May 10 '12 at 10:33
0

This question solves my exact problem:

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

See dmazzoni and primpop's answers.

Community
  • 1
  • 1
hjweide
  • 11,893
  • 9
  • 45
  • 49