Rya*_*art 11
Jersey客户端是远从编码效率的角度来看比HttpClient的更好.考虑:
// Jersey client
WebResource resource = Client.create().resource("http://foo.com")
resource.path("widgets").entity(someWidget).type(APPLICATION_JSON).post();
Wodget wodget = resource.path("widgets/wodget").accept(APPLICATION_XML).get(Wodget.class);
Run Code Online (Sandbox Code Playgroud)
而不是:
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://foo.com/widgets");
HttpEntity someWidgetEntity = ... // something with GSON to marshal the 'someWidget'
httpPost.setEntity(someWidgetEntity);
HttpResponse response = httpclient.execute(httpPost);
HttpGet httpGet = new HttpGet("http://foo.com/widgets/wodget");
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
... // something with GSON to read in the wodget
}
Run Code Online (Sandbox Code Playgroud)
除此之外,Jersey客户端可以使用HttpClient进行HTTP交互,并且您可以充分利用这两个方面:简化的界面以及广泛使用的多功能HTTP客户端库的强大功能.
注意:上面的代码完全未经测试,但形状大致准确.
| 归档时间: |
|
| 查看次数: |
5899 次 |
| 最近记录: |