2014-02-27 56 views
1
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及其所有後續參數。如何在下面的Apache HttpClient代碼上使用OkHttpClient

回答

0

okhttp-apache模塊讓我們這樣做:

HttpClient httpClient = new OkApacheClient(); 

它沒有做的一切的Apache HTTP客戶端可以做的,但它更容易遷移。

+0

是的,它可以創建新的HTTP客戶端,但無法在PUT,POST,GET merhod中執行。它顯示「.excute(put)無效。你能幫我實現這個URL連接嗎? – user3145687

+0

感謝您修復我的錯字,jw! –

+0

請詳細解釋一下嗎? – user3145687

相關問題