Java 如何从restlet ClientResource获取响应?
这可能很容易,但我很困惑 我正在尝试使用Android Restlet在服务器上执行HTTP POST,并读取服务器返回的回复 我使用以下方法创建表单:Java 如何从restlet ClientResource获取响应?,java,android,rest,http-post,restlet,Java,Android,Rest,Http Post,Restlet,这可能很容易,但我很困惑 我正在尝试使用Android Restlet在服务器上执行HTTP POST,并读取服务器返回的回复 我使用以下方法创建表单: Form form = new Form form.add("msg" ,"This is my message"); 现在,我有如下来源: ClientResource clientResource = new ClientResource("www.example.com/my-http-post-url/"); 现在,我正在做HTTP
Form form = new Form
form.add("msg" ,"This is my message");
现在,我有如下来源:
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();
}
我发现代码正在内部尝试,但其打印:
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/)还是卷曲
蒂埃里试试这样: 我现在有类似的事情:
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");
}
我也有这个问题,在纯JavaSE中(不是android)。这就像ResponsePresentation.getText()不起作用。它总是空的。我已经“嗅探”了连接,服务器正在响应体中发送回有效的JSON。我不明白。我最终使用了简单的HTTP POST.:-/它工作得非常好。