在Android工程中引入unirest-java时,运行出现以下错误:
Could not find class ‘javax.naming.ldap.LdapName’, referenced from method com.mashape.relocation.conn.ssl.DefaultHostnameVerifier.extractCN
首先,在Android工程中使用unirest-java需要单独的编译,这里参考官方给出的教程,但使用mvn clean assembly:assembly
命令编译时会在执行Running com.mashape.unirest.test.http.UnirestTest
task是报错,因此需要跳过测试task,以mvn clean assembly:assembly -Dmaven.test.skip=true
命令执行;
当你编译出unirest-java-1.4.10-SNAPSHOT-withDependency-ShadedForAndroid.jar后在Android工程中发现不能使用,会报如上的错误;但使用教程中Sample工程里unirest-java-1.3.2-SNAPSHOT-withDependency-ShadedForAndroid.jar却没问题;
通过查看unirest-java在github的提交历史,发现在adbcfab
的提交记录中修改httpclient/httpasyncclient/httpmime三个jar的版本后,就不能在使用了;因为这里讲三个jar的依赖修改到前一个commit;
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.4</version>
</dependency>
这样重新编译出的jar在Android工程就可以使用了;
原因猜猜是在新版的httpClient中引用了jdk的javax.naming.ldap.LdapName
,而在android.jar中未包含找个类;