Using POST to send data from Android to AppEngine Datastore

与世无争的帅哥 提交于 2021-02-10 05:27:24

问题


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


回答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)




回答2:


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;


}



}



回答3:


This question solves my exact problem:

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

See dmazzoni and primpop's answers.



来源:https://stackoverflow.com/questions/10480136/using-post-to-send-data-from-android-to-appengine-datastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!