Java:如何将String转换为HttpInputMessage

时间:2016-04-28 08:37:17

标签: java string httpresponse

如何将String转换为HttpInputMessage? 或HttpResponse到HttpInputMessage

发布(返回json):

HttpResponse<String> jsonResponse = null;
try {
    jsonResponse = Unirest.post(targetURL).header("Accept", "application/json")
        .header("Content-Type", "application/json;").body(urlParameters).asString();
} catch (UnirestException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
String str = jsonResponse.toString();
HttpInputMessage inputMessage = null;

return inputMessage;

我想将json转换为Object

RoutesList routes = new RoutesList();
Post post = new Post(this.url + allRoutes, depoId.toString());
HttpInputMessage inputMessage = null;
try {
    inputMessage = post.getResult();
} catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
try {
    converter.read(routes.getClass(), RoutesList.class, inputMessage);
} catch (HttpMessageNotReadableException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:0)

您可以使用此类HttpComponentsAsyncClientHttpResponse包装HttpResponse:

HttpComponentsAsyncClientHttpResponse(HttpResponse httpResponse) 

并将其作为HttpInputMessag返回,例如:

return new HttpComponentsAsyncClientHttpResponse(jsonResponse)

此类实现接口HttpInputMessage。