2015-07-21 36 views
1

由於HTTP客戶端相關的類,我有大量的棄用。如何修復使用原生Java庫多個Http *類和方法棄用

這裏我的代碼:

this.httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet(url); 
    HttpResponse response = this.httpclient.execute(httpget); 
    HttpEntity entity = response.getEntity(); 
    int statusCode = response.getStatusLine().getStatusCode(); 
    if (statusCode == HttpStatus.SC_OK) { 
     BufferedReader reader = new BufferedReader(
       new InputStreamReader(entity.getContent(), 
         EntityUtils.getContentCharSet(entity)) 
     ); 
     String line = null; 
     Matcher matcher = null; 
     while ((line = reader.readLine()) != null) { 
      matcher = matcher == null ? this.resultPattern 
        .matcher(line) : matcher.reset(line); 
      if (matcher.matches()) { 
       httpget.abort(); 
       return Double.parseDouble(matcher.group(1)); 
      } 
     } 
     httpget.abort(); 
     throw new MyException("Could not find [" 
       + resultPattern.toString() + "] in response to [" + url 
       + "]"); 
    } else { 
     if (entity != null) { 
      entity.consumeContent(); 
     } 
     throw new MyException("Got [" + statusCode 
       + "] in response to [" + url + "]"); 
    } 

DefaultHttpClient棄用 HttpResponse棄用 HttpEntity棄用

我怎麼能修復使用本地庫?我搜索了一些人HttpClient使用HttpClientBuilder但需要一個額外的庫,此外我不知道如何解決其他折舊問題。

有沒有一些程序員可以幫助我? 這種大規模棄用的原因是什麼?

似乎現在我們應該使用HttpURLConnection,但我還沒有理解如何將我的代碼遷移到這些庫。

回答

-1

如果您正在開發Android版本,您應該已經轉向使用Volley,這會更好。我記得這些方法已被廢棄,並且沒有其他本地庫。

+0

這是一個/意見爲基礎的B /不正確 – njzk2

0

我該如何修復使用本機庫?

我不知道在這種情況下你認爲「本地庫」是什麼。 Android的內置版本的HttpClient已被Android 5.1棄用,並且在M Developer Preview中完全刪除。

如果您需要堅持使用Apache HttpClient API,請切換至Apache's edition of their library

可以切換到任意數量的其他HTTP的API,如內置HttpUrlConnectionOkHttp

這是什麼大規模棄用的原因是什麼?

Google has indicated for a few years that you should not use HttpClient;他們現在只是在執行這個。

+0

感謝您的回答,你能給我根據我的代碼的樣本?看起來與我必須用來執行相同操作的語法完全不同 – AndreaF

+0

OkHttp與HttpClient足夠接近,您可以在代碼中做很少更改的情況下執行相同(基本)的操作,IIRC – njzk2

0

我已經構建了一個使用標準Java庫的HttpUtil類。你可以修改它。我只是將所有內容都設置爲地圖,然後您可以輕鬆發送帖子或獲取。

HttpURLConnection urlConnection; 
String CHAR_ENCODING = "UTF-8"; 
String CONTENT_TYPE_X_WWW_FORM = "application/x-www-form-urlencoded;charset=UTF-8"; 

public String sendHttpPostRequestWithParams(String url, Map<String, Object> httpPostParams) { 
    try { 
     byte[] postDataBytes = setParameters(httpPostParams).toString().getBytes(CHAR_ENCODING); 

     URL post = new URL(url); 
     urlConnection = (HttpURLConnection) post.openConnection(); 
     urlConnection.setDoOutput(true); 
     urlConnection.setRequestProperty("Accept-Charset", CHAR_ENCODING); 
     urlConnection.setRequestProperty("Content-Type", CONTENT_TYPE_X_WWW_FORM); 
     urlConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); 
     urlConnection.getOutputStream().write(postDataBytes); 

     return convertStreamToString(urlConnection.getInputStream()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return Constants.GSON.toJson(new BaseResponse(ResponseCodes.SERVICE_NOT_FOUND.getCode(), 
       ResponseCodes.SERVICE_NOT_FOUND.getMessage())); 
    } 
} 

public String sendHttpGetRequestWithParams(String stringUrl, Map<String, Object> params) { 
    try { 
     URL url = new URL(stringUrl + "?" + setParameters(params)); 
     urlConnection = (HttpURLConnection) url.openConnection(); 

     return convertStreamToString(urlConnection.getInputStream()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return Constants.GSON.toJson(new BaseResponse(ResponseCodes.SERVICE_NOT_FOUND.getCode(), 
       ResponseCodes.SERVICE_NOT_FOUND.getMessage())); 
    } 
} 

public String setParameters(Map<String, Object> params) { 
    try { 
     StringBuilder parameters = new StringBuilder(); 
     for (Map.Entry<String, Object> param : params.entrySet()) { 
      if (parameters.length() != 0) { 
       parameters.append('&'); 
      } 
      parameters.append(URLEncoder.encode(param.getKey(), CHAR_ENCODING)); 
      parameters.append('='); 
      parameters.append(URLEncoder.encode(String.valueOf(param.getValue()), CHAR_ENCODING)); 
     } 
     return parameters.toString(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
     return null; 
    } 
} 

public String convertStreamToString(InputStream inputStream) { 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 
     String temp, response = ""; 
     while ((temp = reader.readLine()) != null) { 
      response += temp; 
     } 
     return response; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 
0

由於谷歌documentation說你仍然可以使用HTTP Apache API接口。聲明以下依賴於你的gradle.build文件:

android { 
    useLibrary 'org.apache.http.legacy' 
} 
相關問題