java.lang.NoSuchFieldError:org.apache.http.message.BasicLineFormatter.INSTANCE [英] java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE

查看:1444
本文介绍了java.lang.NoSuchFieldError:org.apache.http.message.BasicLineFormatter.INSTANCE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我需要帮助!

我开发的应用程序为Android与集成的IIS(的SharePoint)。 当我运行此code(code 2),我得到错误

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

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

我运行此code在firstActivity的方法的onCreate(code 1)

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

版本的HttpClient - 4.3;

Version httpclient - 4.3;

code 1

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

code 2

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.

推荐答案

谷歌Android附带了一个非常过时的(pre BETA 4.0)的Apache的HttpClient叉。由于老班的类路径的presence股票版本的HttpClient无法正确初始化,除非所有的类都移到另外一个名字空间('org.apache.http' - >'thank.you.google.http)

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')

确保股票版本的Apache的HttpClient无法与原来的命名空间重叠的一种方法是用的 Maven的阴影插件,使用端口通过德克柏叶开发,或的的Apache的HttpClient的谷歌Android 的官方移植。

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.

这篇关于java.lang.NoSuchFieldError:org.apache.http.message.BasicLineFormatter.INSTANCE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆