Paul S.
Paul S.

Reputation: 79

GAE Reading from 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

Upvotes: 1

Views: 69

Answers (2)

gipsy
gipsy

Reputation: 3859

The issue is your handler on appengine supporting only http 'GET'( I think you are overriding only doGet) but you are using 'POST' from the client. Change the http method on client side to 'GET'.

Upvotes: 1

Krishna V
Krishna V

Reputation: 1821

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

Upvotes: 1

Related Questions