简体   繁体   English

如何从restlet ClientResource获得响应?

[英]How to get response from restlet ClientResource?

This may be easy one, but I am confused. 这可能很容易,但是我很困惑。

I am trying to do HTTP POST on a server using Android Restlet, and read the reply returned from the server. 我正在尝试使用Android Restlet在服务器上执行HTTP POST,并读取从服务器返回的回复。

I created form using: 我使用以下方法创建了表单:

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

Now, I have clientResource as follows: 现在,我的clientResource如下:

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

Now, I am doing HTTP Post as: 现在,我将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 found out that the code is going inside try, but its printing : 我发现代码在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]

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? 谁能告诉我,如何确定服务器发送的响应内容?

You're using client-side support of Restlet the right way. 您正在以正确的方式使用Restlet的客户端支持。 The response content should be contained within the response representation... 响应内容应包含在响应表示中...

The first step is to call your REST service outside Android to see the exact response content. 第一步是在Android外部调用REST服务以查看确切的响应内容。 Can you try to do this using restclient (http://code.google.com/p/rest-client/) or curl? 您可以尝试使用restclient(http://code.google.com/p/rest-client/)还是curl来做到这一点?

Thierry 蒂埃里

Try something like this: 尝试这样的事情:

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");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM