HTTPClient "main" java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35868242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:41:19  来源:igfitidea点击:

HTTPClient "main" java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>

javajsonsslposthttpclient

提问by dmaprasad

I'm using Httpclient-4.5.2.jar and httpcore-4.4.4.jar HttpClient components and I'm getting below error.

我正在使用 Httpclient-4.5.2.jar 和 httpcore-4.4.4.jar HttpClient 组件,但出现以下错误。

Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:966)

My source code as follows.

我的源代码如下。

try {
        System.out.println("came to try catch");
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost request = new HttpPost("https://bizz.mobilezz.lk/apicall/loanprepaidapi/v1");
        StringEntity params =new StringEntity("{\"mobile\":\"776037285\",\"path\":\"IVV\",\"loanAmount\":\"200000\"}");
        request.addHeader("content-type", "application/json");
        request.addHeader("Authorization", "Bearer casmk34233mlacscmaacsac");

        request.addHeader("Accept", "application/json");        
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);
        System.out.println("response is :"+response.getStatusLine());

    } catch (Exception e) {
        e.printStackTrace();

Please assist me to get rid of this error. I'm trying to send request in post method and get json response.

请帮助我摆脱这个错误。我正在尝试以 post 方法发送请求并获得 json 响应。

回答by dmaprasad

I found an answer to my question and posting for your reference.

我找到了我的问题的答案并发布以供您参考。

HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(
                "https://bizz.mobilezz.lk/apicall/loanprepaidapi/v1");

        JSONObject json = new JSONObject();
        StringEntity params = new StringEntity("{\"msisdn\":\"" + mobile
                + "\",\"channel\":\"SDD\"}");

        new StringEntity(json.toString());
        post.addHeader("Host", "mife.dialog.lk");
        post.addHeader("content-type", "application/json");
        post.addHeader("Authorization", "Bearer " + access_token);
        post.addHeader("Accept", "application/json");

        // System.out.println("status code is:" + status);
        post.setEntity(params);
        HttpResponse response = client.execute(post);
        int status = response.getStatusLine().getStatusCode();
        System.out.println("status code is :" + status);
        resCode = Integer.toString(status);
        if (status != 401) {
            if (status != 409) {
                BufferedReader rd = new BufferedReader(
                        new InputStreamReader(response.getEntity()
                                .getContent()));
                String response1 = readAll(rd);
                System.out.println(response1);
                JSONObject obj = new JSONObject(response1);
                resCode = obj.getString("resCode");
                resDesc = obj.getString("resDesc");
                System.out.println(resCode);
                System.out.println(resDesc);
            }
        }
        System.out.println("reason code is :" + resCode);