1

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.

I created form using:

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

Now, I have clientResource as follows:

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

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();

}

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?

4

2 回答 2

0

您正在以正确的方式使用 Restlet 的客户端支持。响应内容应包含在响应表示中...

第一步是在 Android 之外调用您的 REST 服务以查看确切的响应内容。您可以尝试使用 restclient (http://code.google.com/p/rest-client/) 或 curl 来执行此操作吗?

蒂埃里

于 2012-07-23T14:56:54.320 回答
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");
    }
于 2014-12-22T18:02:39.927 回答