如何在下面的Apache HttpClient代码上使用OkHttpClient

时间:2014-02-27 10:23:30

标签: android okhttp

HttpClient httpClient = new DefaultHttpClient();
         HttpContext localContext = new BasicHttpContext();
         HttpPut put = new HttpPut("url");
         put.addHeader("X-Apikey","");
         StringEntity se = new StringEntity( version.toString());  
         se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
         put.addHeader("Accept", "application/json");
         put.addHeader("Content-type", "application/json");
         put.setEntity(se);


         try{

               HttpResponse response = httpClient.execute(put, localContext);
               HttpEntity entity = response.getEntity();

在这里,我需要一个帮助,用OkHttpClient替换HttpClient及其所有后续参数。

1 个答案:

答案 0 :(得分:0)

okhttp-apache模块让你这样做:

HttpClient httpClient = new OkApacheClient();

它不能完成Apache HTTP客户端可以执行的所有操作,但它确实使迁移更容易。