LG G3手机的Android 6.0 HTTPClient问题

您好我使用DefaultHTTPClient类来创建http请求。

适用于所有手机的执行代码execute()方法适用于所有手机,包括带有Android 6.0的Nexus和Samsung。

但是当我在Android手机上测试Android 6.0更新时,我收到错误,如下所示

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) 

我试图了解什么问题,它谈到了摘要式身份validation。

我从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 

进入类似的东西,并有一个修复我正在尝试,我添加到另一个问题


我今天遇到了类似的问题,刚开始使用HttpClient for Android

  1. 添加了依赖compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'到build.gradle。
  2. HttpClientBuilder.create().build()替换new DefaultHttpClient() HttpClientBuilder.create().build()

您可能需要在代码的其他部分中制作一些其他的小型重构,但这应该非常简单。

给这样的东西一个去,我在运行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'