【问题标题】:GAE Reading from DatastoreGAE 从数据存储中读取
【发布时间】:2015-06-26 09:36:42
【问题描述】:

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

它正在工作:)! 但是当我想读取返回的高分并将字符串添加到 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 代码,例如:

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

但我想要 Paul、1200 Tom、1000 Kevin 800 等等

【问题讨论】:

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


    【解决方案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();
    

    【讨论】:

    • 感谢您的帮助,但您能按照您的方式编写我的方法吗? :) 我不知道在哪里插入你的代码!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-22
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 2014-06-20
      相关资源
      最近更新 更多