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'