android 6.0 httpclient与lg g3手机的问题

kqqjbcuj  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(453)

嗨,我正在使用defaulthttpclient类创建http请求。
current code execute()方法适用于所有手机,包括nexus和三星android 6.0。
但是当我在使用android6.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)

我试图理解什么问题,它涉及到摘要认证。
我知道android6.0已经删除了apachehttp客户端,现在使用httpurlconnection类
我试图将“org.apache.http.legacy.jar”文件从sdk复制到我的project 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
erhoui1w

erhoui1w1#

遇到了类似的问题,我正在尝试一个修正,我在另一个问题中添加了这个修正
我今天遇到了一个类似的问题,刚开始使用httpclient for android
添加了依赖项 compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1' 去建造grad尔。
替换 new DefaultHttpClient()HttpClientBuilder.create().build() 您可能需要在代码的其他部分进行其他一些小的重构,但这应该是非常直接的。

p8ekf7hl

p8ekf7hl2#

试一试,我在运行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的dependencies下

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

相关问题