2010-10-13 26 views
4

我已经编写了一个Web应用程序,使用Restlet框架在Google AppEngine上运行,并使用json与Web客户端进行通信。那些按预期工作。但是,通过Android访问时,编写的一个用于响应Android客户端的特定资源不起作用。但是,它通过网络浏览器访问时可以正常工作(我不会从浏览器发送请求参数,因此在这种情况下可以获得400这是可以的)。在Google AppEngine上运行已部署的应用程序的问题

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设备运行验证码:

此代码对DevAppServer运行时,工作

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客户端,服务器没有按”不要写任何建议请求没有到达的日志消息。

+0

无响应的App Engine应用程序通常是由错误配置的app.yaml描述和/或缺少的处理程序引起的。你可以在这里包括这两个?这将有助于诊断问题。 – Greg 2011-04-11 19:18:44

+0

您是否尝试过我的测试? – Panthro 2011-05-06 21:57:57

回答

0

看来,它更是一个的AppEngine/Java的问题不是一个机器人的问题,但...让我们尝试别的东西:

,而不是使用客户端和u所使用的东西,最初只是尝试,看看有什么服务器响应最简单的连接(如你在web浏览器一样):

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

如果一切正常,你会得到你的expeted 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); 

让我知道答案是否有用

相关问题