java.lang.NoSuchFieldError:org.apache.http.message.BasicLineFormatter.INSTANCE

美好的一天!

我需要帮助!

我开发应用程序的Android与集成的IIS(SharePoint)。 而当我运行这个代码(代码2),我得到错误

java.lang.NoSuchFieldError:org.apache.http.message.BasicLineFormatter.INSTANCE

在onCreate(代码1)的方法firstActivity中运行此代码

版本httpclient – 4.3;

代码1

new Thread(new Runnable() { @Override public void run() { new HttpsClietn(); } }).run(); 

代码2

 package com.example.HttpsMy; import android.os.Environment; import org.apache.http.auth.AuthScope; import org.apache.http.auth.NTCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.HttpClientBuilder; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.*; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class HttpsClietn { HttpsClietn() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new NTCredentials("Grigoriy.Polyakov","password", "", "domain.kz")); HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); HttpGet httpget = new HttpGet("https://serveer.domain.kz"); HttpClient client = HttpClientBuilder.create() .setSSLSocketFactory(getFactory()) .setDefaultCredentialsProvider(credsProvider) .setSslcontext(getContext()) .build(); System.out.println(client.execute(httpget,context).getStatusLine()); } SSLContext getContext() throws NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException, CertificateException, UnrecoverableKeyException { //new File("key/keystore.p12"), "1234" KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); KeyStore keyStore = KeyStore.getInstance("PKCS12"); InputStream keyInput = new FileInputStream(new File(Environment.getExternalStorageDirectory().getPath()+"/kvv/keystore.p12")); keyStore.load(keyInput, "1234".toCharArray()); keyInput.close(); keyManagerFactory.init(keyStore, "1234".toCharArray()); TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; SSLContext context = SSLContext.getInstance("TLS"); context.init(keyManagerFactory.getKeyManagers(), trustAllCerts, new SecureRandom()); return context; } SSLConnectionSocketFactory getFactory() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException { return new SSLConnectionSocketFactory(getContext()); } } 

谢谢你的帮助。

Solutions Collecting From Web of "java.lang.NoSuchFieldError:org.apache.http.message.BasicLineFormatter.INSTANCE"

谷歌Android与Apache HttpClient的一个非常过时的(pre-BETA 4.0)分叉。 由于类path中存在旧类,除非所有类都移动到另一个名称空间('org.apache.http' – >'thank.you.google.http'),否则HttpClient的股票版本将无法正确初始化

确保Apache HttpClient的股票版本与原来的命名空间不重叠的一种方法是用Maven Shade插件重新打包,使用由Dirk Boye开发的端口或者Apache HttpClient的官方端口 。

对于android删除所有的httpclient和httpcore的依赖关系,并添加

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

在你的build.gradle文件中