GAE Reading from Datastore

时间:2015-06-26 09:36:42

标签: android google-app-engine google-cloud-datastore

I want to return myHighscores from Datastore: i.e.: Paul,1200 Tom,1000 Kevin,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"));
    }
}

and it is working :) ! But when I want to read the returned Highscores and add the String to a textView with:

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);

I only get some HTML code like:

><html><head><meta http-euiv="content-type"content="text/html;charset=utf-8"><title>405 GTTP method POST is....

But i want something like Paul,1200 Tom,1000 Kevin 800 and so on

2 个答案:

答案 0 :(得分:1)

HttpPost not accepted query parameter, as like "?game=" + HIGHSCORESERVER_GAME_ID.

you need to pass that values as

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();

答案 1 :(得分:1)

问题是你的appengine处理程序只支持http'GET'(我认为你只覆盖了doGet),但是你正在使用来自客户端的'POST'。将客户端的http方法更改为“GET”。