Android 6.0 HTTPClient issue with LG G3 phone

匆匆过客 提交于 2019-12-03 15:20:04

问题


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

回答1:


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.




回答2:


Give something like this a go, i had similar problem on MotoX running 6.0.1 but this seem to work for me.

 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;
    }
}

Also stick this into the gradle under dependencies

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


来源:https://stackoverflow.com/questions/36262232/android-6-0-httpclient-issue-with-lg-g3-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!