android无法发送请求,尽管清单中启用了明文通信

lx0bsm1f  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(179)

我一直在使用unirest调用天气api(https://rapidapi.com/community/api/open-weather-map/)在尝试了许多常见的解决方案之后,它仍然提供了一个错误。
下面是对产生错误的原因的重新创建。

// Java
    @RequiresApi(api = VERSION_CODES.O)
    public void GetWeather(View view) throws UnirestException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                HttpResponse<JsonNode> response = Unirest.get("https://community-open-weather-map.p.rapidapi.com/find?q=london&cnt=0&mode=null&lon=0&type=link%2C%20accurate&lat=0&units=imperial%2C%20metric")
                        .header("x-rapidapi-key", "<KEY>")
                        .header("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com")
                        .asJson();

                System.out.println(response.getBody());
                System.out.println("Test");
            }
        });
        thread.start();
    }

以及由此产生的错误:
2021-07-03 20:59:40.553 15665-15783/com.example.weather/androidruntime:致命异常:线程2进程:com.example.weather,pid:15665 java.lang.nosuchfielderror:没有lorg/apache/http/conn/ssl/AllowlHostNameVerifier类型的静态字段示例;lorg/apache/http/conn/ssl/AllowlHostNameVerifier类中;或者它的超类(声明'org.apache.http.conn.ssl.allowallhostnameverifier'出现在/system/framework/framework.jar!classes4.dex),位于org.apache.http.conn.ssl.sslconnectionsocketfactory.(sslconnectionsocketfactory.java:151),位于org.apache.http.conn.ssl.sslconnectionsocketfactory.getsocketfactory(sslconnectionsocketfactory.java:194),位于位于kong.unirest.apache.apache.apacheclient.(apacheclient.java:54)的kong.unirest.apache.securityconfig.createmanager(securityconfig.java:59)的kong.unirest.apache.apache.securityconfig.buildsocketfactory(securityconfig.java:75)在kong.unirest.-$$lambda$lzmzt6m8siumve\u yoizrwh8.apply(未知来源:4)在kong.unirest.config.buildclient.config.java:755)在com.example.weather.mainactivity$1.run(mainactivity.java:43)java.lang.thread.run(thread.java:923)2021-07-03 20:59:40.645 15665-15783/com.example.weather i/进程:发送信号。pid:15665信号:9
我试图解决的问题是:
从上面可以看出,我首先尝试了线程化请求
然后是@requiresapi(api=version\u codes.o)
在androidmanifest中,我添加了:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    android:usesCleartextTraffic="true">

并制作了一个网络_security_config.xml,其中包含:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" >
    </base-config>
</network-security-config>

为了检查请求是否有错误,我在intellij中运行了它,它能够返回数据,那么我遗漏了什么呢?
或者我不应该将unirest与android一起使用,我在过去使用过改进,虽然unirest似乎更简洁,但这已经奏效。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题