2015-06-26 81 views
1

我想從數據存儲返回myHighscores: 即:保羅,1200 湯姆,1000 凱文,800GAE閱讀從數據存儲

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

,它是工作:)! 但是,當我想讀返回榜和字符串添加到一個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....

但我想是保羅,1200湯姆,1000凱文·800等

回答

1

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

Thx的幫助,但你可以寫你的方式嗎? :)我不知道在哪裏插入你的代碼! –

+0

查看我的更新回答 –

+0

thx dude現在工作得很好! –

1

的問題是你的處理程序在appengine上只支持http'GET'(我認爲你只覆蓋doGet),但是你正在使用客戶端的'POST'。將客戶端的http方法更改爲'GET'。