[英]How to get response from 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 Post做為:
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();
}
我發現代碼在try里面,但是可以打印:
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]
我嘗試嗅探數據,以查看服務器是否正在答復,我確信服務器正在發送響應內容。
誰能告訴我,如何確定服務器發送的響應內容?
您正在以正確的方式使用Restlet的客戶端支持。 響應內容應包含在響應表示中...
第一步是在Android外部調用REST服務以查看確切的響應內容。 您可以嘗試使用restclient(http://code.google.com/p/rest-client/)還是curl來做到這一點?
蒂埃里
嘗試這樣的事情:
我現在有類似的東西:
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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.