Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

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

Good day!

I need help!

I develop app for android with integration the IIS(SharePoint). And when i run this code(Code 2), i get ERROR

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

I run this code in firstActivity in method onCreate(Code 1)

Version httpclient - 4.3;

Code 1

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

Code 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());
    }
}

Thanks for any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Google Android ships with an extremely outdated (pre BETA 4.0) fork of Apache HttpClient. Due to the presence of old classes on the classpath the stock version of HttpClient fails to initialize correctly unless all classes are moved to another namespace ('org.apache.http' -> 'thank.you.google.http')

One way of making sure the stock version of Apache HttpClient does not overlap with its original namespace is repackaging it with the Maven Shade plugin, use the port developed by Dirk Boye, or the official port of Apache HttpClient to Google Android.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...