“线程中的异常”;AWT-EventQueue-0“;java.lang.NoSuchFieldError:DEF_CONTENT_CHARSET

“线程中的异常”;AWT-EventQueue-0“;java.lang.NoSuchFieldError:DEF_CONTENT_CHARSET,java,rest,api,jersey,apache-commons-httpclient,Java,Rest,Api,Jersey,Apache Commons Httpclient,我正在尝试从JavaSwing桌面应用程序(在jersey web应用程序上实现)调用登录api,使用下面的代码在标题中使用用户id和密码 String authString = username + ":" + password; String authStringEnc = new BASE64Encoder().encode(authString.getBytes()); System.out.println("Base64 encoded auth string: " + a

我正在尝试从JavaSwing桌面应用程序(在jersey web应用程序上实现)调用登录api,使用下面的代码在标题中使用用户id和密码

  String authString = username + ":" + password;
  String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
  System.out.println("Base64 encoded auth string: " + authStringEnc);

  DefaultHttpClient httpClient = new DefaultHttpClient();
  HttpGet getRequest = new HttpGet(FVConstants.loginAPI);
  getRequest.addHeader("Authorization", "Basic " + authStringEnc);

  HttpResponse response = null;
  try {
    response = httpClient.execute(getRequest);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
           + response.getStatusLine().getStatusCode());


    }

    BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
            System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
private void callLoginApi(String username, String password)
{
  String authString = username + ":" + password;
  String authStringEnc = new 
  BASE64Encoder().encode(authString.getBytes());
  System.out.println("Base64 encoded auth string: " + authStringEnc);

  HttpClient client = HttpClientBuilder.create().build();
  HttpGet request = new HttpGet(FVConstants.loginAPI);
  request.addHeader("Authorization", "Basic " + authStringEnc);

  try {
      HttpResponse response = client.execute(request);
      System.out.println("response is : "+response.getStatusLine());

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

  }
}
它在下面的
response=httpClient.execute(getRequest)行给出了一个错误

我试图从构建路径和/.m2/repository/org/apache/中删除同一个库的多个版本,如中所述,并再次添加,但仍然出现相同的错误。
非常感谢您的帮助。

使用下面的代码解决。请确保您有以下导入

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.HttpResponse;
使用这些导入添加库,您可以很好地使用下面的代码

  String authString = username + ":" + password;
  String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
  System.out.println("Base64 encoded auth string: " + authStringEnc);

  DefaultHttpClient httpClient = new DefaultHttpClient();
  HttpGet getRequest = new HttpGet(FVConstants.loginAPI);
  getRequest.addHeader("Authorization", "Basic " + authStringEnc);

  HttpResponse response = null;
  try {
    response = httpClient.execute(getRequest);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
           + response.getStatusLine().getStatusCode());


    }

    BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
            System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
private void callLoginApi(String username, String password)
{
  String authString = username + ":" + password;
  String authStringEnc = new 
  BASE64Encoder().encode(authString.getBytes());
  System.out.println("Base64 encoded auth string: " + authStringEnc);

  HttpClient client = HttpClientBuilder.create().build();
  HttpGet request = new HttpGet(FVConstants.loginAPI);
  request.addHeader("Authorization", "Basic " + authStringEnc);

  try {
      HttpResponse response = client.execute(request);
      System.out.println("response is : "+response.getStatusLine());

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

  }
}

希望这个链接能帮助你,我遇到了与我尝试的解决方案相同的链接