如何获得的Restlet ClientResource回应? [英] How to get response from restlet ClientResource?
问题描述
这可能是容易的,但我很困惑。
This may be easy one, but I am confused.
我试图做的采用Android的Restlet的服务器上HTTP POST,并读取从服务器返回的答复。
I am trying to do HTTP POST on a server using Android Restlet, and read the reply returned from the server.
我使用创建的窗体:
Form form = new Form
form.add("msg" ,"This is my message");
现在,我有clientResource如下:
Now, I have clientResource as follows:
ClientResource clientResource = new ClientResource("www.example.com/my-http-post-url/");
现在,我做的HTTP POST如下:
Now, I am doing HTTP Post as:
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();
}
我发现了code是怎么回事里面尝试,但其打印:
I found out that the code is going inside try, but its printing :
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]
我试过嗅探数据,看看如果服务器回复与否,我相信,服务器发送响应内容。
I tried sniffing the data, to see if the server is replying or not, I am confident that server is sending the response content.
谁能告诉我,我怎么能找出响应内容服务器发送?
Can anyone tell me, how can I find out the response content send by the server?
推荐答案
尝试是这样的:
我也有类似的到了现在这个不对头:
I have something similar to this right now:
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");
}
这篇关于如何获得的Restlet ClientResource回应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!