【发布时间】: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 描述和/或缺少处理程序引起的。你能把这两个都包括在这里吗?这将有助于诊断问题。