您好我使用DefaultHTTPClient类来创建http请求。
目前的代码execute()方法适用于所有手机,包括Nexus和三星Android 6.0。
但是当我在安装了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
答案 0 :(得分:2)
进入类似的东西并进行修复,我正在尝试将其添加到another question
我今天遇到了类似的问题,刚开始使用HttpClient for Android
var d = new Dictionary();
d.Add("dog", "animal");
....
d["dog"]; //this gives you animal.
替换为compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
在代码的其他部分可能还需要制作一些其他的小型重构,但这应该非常简单。
答案 1 :(得分: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'