我正在学习基于JAVA的Android应用和谷歌应用引擎。 (我实际上正在使用留言板示例“
我正在尝试构建Android应用,它可以在谷歌应用引擎的简单网页上发布简单的消息。
例如,我只是运行Android应用程序,然后它会自动在谷歌引擎上的网页上发布问候消息。
首先,在服务端“google app engine”。我有一个doPost函数,比如..
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
// We have one entity group per Guestbook with all Greetings residing
// in the same entity group as the Guestbook to which they belong.
// This lets us run a transactional ancestor query to retrieve all
// Greetings for a given Guestbook. However, the write rate to each
// Guestbook should be limited to ~1/second.
String guestbookName = req.getParameter("guestbookName");
Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName);
String content = req.getParameter("content");
Date date = new Date();
Entity greeting = new Entity("Greeting", guestbookKey);
greeting.setProperty("user", user);
greeting.setProperty("date", date);
greeting.setProperty("content", content);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(greeting);
resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName);
}
我目前不关心用户和日期。我只想通过andriod app更新内容。
其次,在html页面上我有..
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Post Greeting" /></div>
<input type="hidden" name="guestbookName" value="default"/>
</form>
第三,我在网页上构建了Android应用程序的更新数据
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost("http://example.appspot.com/");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("content", "Helloasdas"));
httpost.setEntity(new UrlEncodedFormEntity(nvps));
new Timer();
Timer.reset();
Timer.start();
response = httpclient.execute(httpost);
entity = response.getEntity();
Timer.stop();
entity.consumeContent();
}
但是,我不确定为什么不在“http://example.appspot.com/”上发布meessage。
我是否错过了doPost或andriod部分的一部分?