2012-07-22 78 views
1

這可能很容易,但我很困惑。如何從restlet ClientResource獲取響應?

我試圖做採用Android的Restlet的服務器上HTTP POST,並讀取從服務器返回的答覆。

我創建的形式使用:

Form form = new Form 
form.add("msg" ,"This is my message"); 

現在,我有clientResource如下:

ClientResource clientResource = new ClientResource("www.example.com/my-http-post-url/"); 

現在,我做的HTTP郵政爲:

Representation response=null; 

try{ 

response= clientResource.post(form.getWebRepresentation(null)); 
System.out.println("Got response !! , response : " + response.getText()); 
System.out.println("Got Context: " + clientResource.getContext()); 
System.out.println("Got Response: " + clientResource.getResponse()); 
System.out.println("Got Resonse Attribute : " + clientResource.getResponseAttributes()); 
System.out.println("Got Resonse Entity: " + clientResource.getResponseEntity()); 

}catch(Exception e){ 

e.printStackTrace(); 

} 

我發現代碼正在嘗試,但它的打印:

I/org.restlet( 493): Starting the default HTTP client 
I/System.out( 493): Got response !! , response : null 
I/System.out( 493): Got Context: null 
I/System.out( 493): Got Response: HTTP/1.1 - OK (200) - OK 
I/System.out( 493): Got Resonse Attribute : {org.restlet.http.headers=[(Date,Sun, 22 Jul 2012 22:14:03 GMT), (Server,WSGIServer/0.1 Python/2.7.1+), (Vary,Authorization), (Content-Type,application/json; charset=utf-8)]} 
I/System.out( 493): Got Resonse Entity: [application/json,UTF-8] 

我嘗試嗅探數據,看看如果服務器回覆與否,我相信,服務器發送的響應內容。

有誰能告訴我,我怎麼能找出服務器發送的響應內容?

+1

我也有這個問題,只是普通的Java SE(不是機器人) 。這就像responseRepresentation.getText()不起作用。它始終爲空。我已經「嗅探」了連接,並且服務器正在響應正文中發回有效的JSON。我不明白。 – GroovyCakes 2012-10-29 21:37:58

+0

我結束了使用簡單的HTTP POST。 : - /它工作絕對好。 – d34th4ck3r 2012-10-29 23:41:01

回答

0

您正以正確的方式使用Restlet的客戶端支持。響應內容應包含的響應表示中......

的第一步是調用的Android之外的REST服務,看看具體的響應內容。你可以嘗試使用restclient(http://code.google.com/p/rest-client/)或curl來做到這一點嗎?

蒂埃裏

0

嘗試是這樣的:

我有類似這樣的,現在的東西:

ClientResource clientResource = new ClientResource(url); 
Request request = new Request(Method.POST, url); 

clientResource.setRequest(request); 

    Form form = new Form(); 

    form.set("foo", "barValue"); 

    org.restlet.representation.Representation response = clientResource.post(form, MediaType.APPLICATION_JSON); 
    Representation responseEntity = clientResource.getResponseEntity(); 

    JsonRepresentation jsonRepresentation = new JsonRepresentation(responseEntity); 

    JSONObject jsonObject = jsonRepresentation.getJsonObject(); 
    String[] names = JSONObject.getNames(jsonObject); 

    if (jsonObject.has("errorString")) 
    { 
     String error = jsonObject.optString("errorString"); 
    }