Sendgrid Android插件初始化错误“ java.lang.NoSuchFieldError:类型... / AllowAllHostnameVerifier没有静态字段实例”

时间:2019-07-31 09:08:51

标签: android sendgrid

我正在尝试Android的Sendgrid插件API。我正在Sendgrid网站上进行以下操作,该操作以以下结尾:

import com.sendgrid.*;
import java.io.IOException;

public class Example {
  public static void main(String[] args) throws IOException {
    Email from = new Email("test@example.com");
    String subject = "Sending with SendGrid is Fun";
    Email to = new Email("test@example.com");
    Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
    Mail mail = new Mail(from, subject, to, content);

    SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
    Request request = new Request();
    try {
      request.setMethod(Method.POST);
      request.setEndpoint("mail/send");
      request.setBody(mail.build());
      Response response = sg.api(request);
      System.out.println(response.getStatusCode());
      System.out.println(response.getBody());
      System.out.println(response.getHeaders());
    } catch (IOException ex) {
      throw ex;
    }
  }
}

此代码在以下位置产生错误

SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));

of

java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar!classes3.dex)
        at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
        at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:966)
        at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
        at com.sendgrid.Client.<init>(Client.java:56)
        at com.sendgrid.SendGrid.<init>(SendGrid.java:52)
        ...

other answer中,我发现自API v22起不推荐使用AllowAllHostnameVerifier。但是我对Android没有足够的了解,无法玩弄第三方库并将其更改为HTTPUrlConnection。

我还偶然发现this sendgrid library将该特定的网络库更改为Http库,但是当我尝试运行时,在httpclient-android-4.3.5.1.jar(org .apache.httpcomponents:httpclient-android:4.3.5.1)和httpcore-4.3.2.jar(org.apache.httpcomponents:httpcore:4.3.2)。

我该如何解决或解决此问题?我使用了最新的Sendgrid插件(4.4.1),但仍然弹出相同的问题。

0 个答案:

没有答案