2010-10-13 27 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); 

讓我知道答案是否有用

相關問題