Sunil Sunil - 6 months ago 102
Java Question

Android 6.0 HTTPClient issue with LG G3 phone

Hi I am using DefaultHTTPClient class to create http request.

The currrent code execute() method works on all phones including Nexus and Samsung with Android 6.0.

But when i tested on LG phones with Android 6.0 update, i am getting error as shown below

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)


I tried to understand what issue, It talks about Digest authentication.

I know from Android 6.0 they have removed Apache Http client and now uses HTTPUrlConnection class

I tried to copy the "org.apache.http.legacy.jar" file from sdk to my project lib folder.

But still i face the same error log. I hope someone can help me to get wor around with this issue.

Kindly find the application code as it is :

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

Answer

Ran into something similar and had a fix I'm trying that I added to another question


I ran into a similar issue today and just started using HttpClient for Android

  1. Added dependency compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1' to build.gradle.
  2. Replace new DefaultHttpClient() with HttpClientBuilder.create().build()

There are probably some other minor refactors you might need to make in other portions of the code, but that should be pretty straight forward.