GAE 从数据存储中读取
GAE Reading from Datastore
我想 return 来自 Datastore 的 myHighscores:
即:保罗,1200
汤姆,1000
凯文,800
private void returnHighscores(HttpServletResponse resp, String game, int max) throws IOException {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key gameKey = KeyFactory.createKey("game", game);
Query query = new Query("highscore", gameKey);
query.addSort("points", Query.SortDirection.DESCENDING);
List<Entity> highscores = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(max));
for(Entity e : highscores) {
resp.getWriter().println(e.getProperty("name") + "," + e.getProperty("points"));
}
}
它正在运行 :)!
但是当我想阅读 returned Highscores 并将字符串添加到 textView 时:
AndroidHttpClient client = AndroidHttpClient.newInstance("Mueckenfang");
HttpPost request = new HttpPost(HIGHSCORE_SERVER_BASE_URL + "?game=" + HIGHSCORESERVER_GAME_ID);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStreamReader reader = new InputStreamReader(entity.getContent(), "utf-8");
int c = reader.read();
while (c > 0) {
highscores += (char) c;
c = reader.read();
}
TextView tv = (TextView) findViewById(R.id.highscoreTv);
tv.setText(highscores);
我只得到一些 HTML 代码,例如:
><html><head><meta http-euiv="content-type"content="text/html;charset=utf-8"><title>405 GTTP method POST is....
但我想要 Paul,1200 Tom,1000 Kevin 800 等等
HttpPost 不接受查询参数,
就像 "?game=" + HIGHSCORESERVER_GAME_ID.
您需要将这些值作为
传递
AndroidHttpClient client = AndroidHttpClient.newInstance("Mueckenfang");
HttpPost request = new HttpPost(HIGHSCORE_SERVER_BASE_URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("game", String.valueOf(HIGHSCORESERVER_GAME_ID)));
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
问题是您在 appengine 上的处理程序仅支持 http 'GET'(我认为您只覆盖了 doGet)但您使用的是来自客户端的 'POST'。将客户端的 http 方法更改为 'GET'.
我想 return 来自 Datastore 的 myHighscores: 即:保罗,1200 汤姆,1000 凯文,800
private void returnHighscores(HttpServletResponse resp, String game, int max) throws IOException {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key gameKey = KeyFactory.createKey("game", game);
Query query = new Query("highscore", gameKey);
query.addSort("points", Query.SortDirection.DESCENDING);
List<Entity> highscores = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(max));
for(Entity e : highscores) {
resp.getWriter().println(e.getProperty("name") + "," + e.getProperty("points"));
}
}
它正在运行 :)! 但是当我想阅读 returned Highscores 并将字符串添加到 textView 时:
AndroidHttpClient client = AndroidHttpClient.newInstance("Mueckenfang");
HttpPost request = new HttpPost(HIGHSCORE_SERVER_BASE_URL + "?game=" + HIGHSCORESERVER_GAME_ID);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStreamReader reader = new InputStreamReader(entity.getContent(), "utf-8");
int c = reader.read();
while (c > 0) {
highscores += (char) c;
c = reader.read();
}
TextView tv = (TextView) findViewById(R.id.highscoreTv);
tv.setText(highscores);
我只得到一些 HTML 代码,例如:
><html><head><meta http-euiv="content-type"content="text/html;charset=utf-8"><title>405 GTTP method POST is....
但我想要 Paul,1200 Tom,1000 Kevin 800 等等
HttpPost 不接受查询参数,
就像 "?game=" + HIGHSCORESERVER_GAME_ID.
您需要将这些值作为
传递AndroidHttpClient client = AndroidHttpClient.newInstance("Mueckenfang");
HttpPost request = new HttpPost(HIGHSCORE_SERVER_BASE_URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("game", String.valueOf(HIGHSCORESERVER_GAME_ID)));
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
问题是您在 appengine 上的处理程序仅支持 http 'GET'(我认为您只覆盖了 doGet)但您使用的是来自客户端的 'POST'。将客户端的 http 方法更改为 'GET'.