2

您好我正在使用DefaultHTTPClient類來創建http請求。用LG G3手機Android 6.0 HTTPClient問題

currrent code execute()方法適用於包括Nexus和Samsung在內的所有手機。

但是當我到Android 6.0的更新上LG手機測試,如下圖所示

Java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 
03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.auth.DigestScheme.isGbaScheme(DigestScheme.java:210) 
03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.auth.DigestScheme.processChallenge(DigestScheme.java:176) 
03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.DefaultRequestDirector.processChallenges(DefaultRequestDirector.java:1097) 
03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:980) 
03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:490) 
03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560) 
03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492) 
03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470) 
03-28 17:21:17.040: E/xx_SDK(14035): at java.lang.Thread.run(Thread.java:818) 

我想明白了什麼問題,它談論摘要式身份驗證,我收到錯誤。

我知道在Android 6.0,他們已刪除了Apache的HTTP客戶端,現在使用HttpURLConnection的類

我試圖「org.apache.http.legacy.jar」文件從SDK複製到我的項目lib文件夾。

但我仍然面對相同的錯誤日誌。我希望有人能幫助我解決這個問題。

請查找應用程序代碼,因爲它是:

HttpParams params = new BasicHttpParams(); 
ClientConnectionManager connectionManager = null; 
try { 
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
trustStore.load(null, null); 
SchemeRegistry registry = new SchemeRegistry(); 
SSLSocketFactory sf = new MySSLSocketFactory(trustStore); 
sf.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);  
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
registry.register(new Scheme("https", sf, 443)); 
connectionManager = new SingleClientConnManager(params, registry); 
} 
catch (Exception e) 
{ 
    Log.e(TAG, Log.getStackTraceString(e)); 
} 

ConnManagerParams.setTimeout(params, mTimeOut); 
HttpConnectionParams.setSoTimeout(params, mTimeOut); 
HttpConnectionParams.setConnectionTimeout(params, mTimeOut); 
HttpConnectionParams.setTcpNoDelay(params, true); 

DefaultHttpClient client = new DefaultHttpClient(connectionManager, params); 

client.getCredentialsProvider().setCredentials(new AuthScope(host,port), 
new UsernamePasswordCredentials(userID,passowrd)); 

client.setRedirectHandler(new RedirectHandler() { 
@Override 
public boolean isRedirectRequested(HttpResponse response, HttpContext context) { 
} 
@Override 
public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException { 
} 

try { 
HttpGet httpget = new HttpGet(url); 
HttpResponse response = client.execute(httpget); // issue occur here 

回答

2

跑進類似的東西,有一個修復我想,我加入到another question


我遇到了一個類似的問題今天剛剛開始使用HttpClient for Android

  1. 增加的依賴關係compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1' build.gradle。
  2. 更換new DefaultHttpClient()HttpClientBuilder.create().build()

有可能是你可能需要在代碼中的其他部分做出一些小的refactors,但應該是相當直截了當。

+0

我使用的庫按了Android 6.0規範。 useLibrary「org.apache.http.legacy.jar」。我所看到的是它發送了http請求。服務器迴應401代碼,我們再次發送Digest認證。如果您看到錯誤日誌,我覺得Digest參數有問題。同時你能告訴我如何使用你的新代碼。我無法理解如何更改DefaultClient。您的第二點不明確 – Sunil

+0

我也在使用舊版jar,但仍然只在LG G5和LG G3 *上出現錯誤*。我沒有看到示例代碼,所以我假設你有一些地方使用apache庫中的'DefaultHttpClient'。如果添加新的依賴關係,那麼在發出請求之前,您需要更新您使用'DefaultHttpClient'的代碼來使用'HttpClientBuilder'。這將使用'httpclient-android'中的新代碼,並且應該正確處理摘要參數。 – jfred

+0

非常感謝您的意見。我很高興看到你的支持。這對我來說是一個非常關鍵的問題。根據您的評論,我已更新我的應用程序代碼。現在你可以看到上面的代碼。KIndly支持我如何根據HttpClientBuilder更改此代碼。 – Sunil

0

給出這樣的事情,我在運行6.0.1的MotoX上遇到了類似的問題,但這似乎對我很有用。

protected static final int HTTP_JSON_TIMEOUT_CONNECTION = 20000; 
protected static final int HTTP_JSON_TIMEOUT_SOCKET = 20000; 

CloseableHttpResponse response = null; 
    try { 
     UserPrefs.clearCacheFolder(mContext.getCacheDir()); 
     CloseableHttpClient httpClient = getApacheHttpClient(); 
     HttpPost httpPost = getHttpPost(invocation); 

     response = httpClient.execute(httpPost); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 


    protected CloseableHttpClient getApacheHttpClient(){ 
    try { 
     // Socket config 
     SocketConfig socketConfig = SocketConfig.custom() 
       .setSoTimeout(HTTP_JSON_TIMEOUT_SOCKET) 
       .build(); 
     // Auth 
     CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); 
     credentialsProvider.setCredentials(new AuthScope(URL, PORT, null, "Digest"), 
       new UsernamePasswordCredentials(USERNAME,DIGEST_PASSWORD)); 
     // Build HttpClient 
     return HttpClients.custom() 
       .setDefaultSocketConfig(socketConfig) 
       .setDefaultCredentialsProvider(credentialsProvider) 
       .build(); 
    }catch (Exception e) { 
     Log.i("ApacheHttpClientHelper", "ERROR >> "+e.getMessage()); 
     return null; 
    } 
} 

而且堅持到這一點的gradle這個下依賴

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'