【问题标题】:Problems running deployed apps on Google AppEngine在 Google AppEngine 上运行已部署的应用程序时出现问题
【发布时间】:2011-04-24 22:18:09
【问题描述】:

我编写了一个使用 Restlet 框架在 Google AppEngine 上运行的 Web 应用程序,使用 json 与 Web 客户端进行通信。这些按预期工作。但是,为向 Android 客户端提供响应而编写的一种特定资源在通过 Android 访问时不起作用。但是,当通过网络浏览器访问时它确实有效(我没有从浏览器发送请求参数,因此得到 400,在这种情况下是可以的)。

此代码在 DevAppServer 上运行时有效:

public class PlayResource extends ServerResource {
private final float SCOREBASE = 1000.0F;

@Get
@Post
public JsonRepresentation play() {
    try {
        JsonRepresentation rep = new JsonRepresentation(getRequestEntity());
        JSONObject inputJson = rep.getJsonObject();
        JSONObject outputJson = new JSONObject();

        String country = inputJson.optString("country");
        outputJson.put("country", doSomething("country",country));
                    ......
        ......
        return new JsonRepresentation(outputJson);
    } catch (IOException e) {
        try {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return new JsonRepresentation(
                    new JSONObject()
                        .put(Messages.TYPE_ERROR, Messages.BAD_REQUEST));
        } catch (JSONException e2) {
            setStatus(Status.SERVER_ERROR_INTERNAL);
            return null;
        }
    } catch (JSONException e) {
        try {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return new JsonRepresentation(
                    new JSONObject()
                        .put(Messages.TYPE_ERROR, Messages.BAD_FORMAT));
        } catch (JSONException e2) {
            setStatus(Status.SERVER_ERROR_INTERNAL);
            return null;
        }
    }
}

}

并且客户端 Android 设备正在运行此代码:

  Client client = new Client(Protocol.HTTP);
    try {
        JsonRepresentation requestJSON = new JsonRepresentation(new JSONObject()
            .put("country", country.trim())
        );
        Request req = new Request(Method.GET,"http://****.appspot.com/resource/play",requestJSON);
        Response resp = client.handle(req);
        String res = resp.getEntity().getText();
        JSONObject resultJSON = new JSONObject(res);

运行此请求只会挂起 Android 客户端,服务器不会写入任何日志消息,表明请求未到达那里。

【问题讨论】:

  • 无响应的 App Engine 应用通常是由错误配置的 app.yaml 描述和/或缺少处理程序引起的。你能把这两个都包括在这里吗?这将有助于诊断问题。

标签: android google-app-engine


【解决方案1】:

这似乎更像是一个 Appengine/Java 问题而不是一个 android 问题,但是......让我们试试别的:

不要使用客户端和您正在使用的东西,首先尝试查看服务器对最简单的连接的响应(就像您在网络浏览器中所做的那样):

        URL url;
        try {
            url = new URL("http://yourappid.appspot.com/resource/play");
            String content = (String) url.getContent();
            System.out.println(content);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

如果它有效并且您获得了 400,如果是这样...尝试发送带有数据的 httppostrequest... 像这样:

HttpClient client = new DefaultHttpClient();
HttpUriRequest httpRequest = new HttpPost("http://yourappid.appspot.com/resource/play");
//set the content type to json
httpRequest.setHeader("Content-Type", "application/json");
//get and work with the response
HttpResponse httpResponse = client.execute(httpRequest);

如果答案有用,请告诉我

【讨论】:

    猜你喜欢
    • 2017-02-12
    • 2020-02-06
    • 2013-09-28
    • 2013-09-30
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    相关资源
    最近更新 更多