Instant app CLEARTEXT communication not supported: []

浪子不回头ぞ 提交于 2020-01-04 05:39:06

问题


I tried to do HttpUrlConnection, but got Exception:

    java.net.UnknownServiceException: CLEARTEXT communication not supported: []
    at com.android.okhttp.Connection.connect(Connection.java:149)
    at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
    at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
    at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
    at com.xfzj.instantappdemo2.feature.MainActivity$3.run(MainActivity.java:84)
    at java.lang.Thread.run(Thread.java:818)

Here's my code:

new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL("http://www.google.com");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                Log.i("responseCode",""+connection.getResponseCode());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
    }).start();

If I ran and installed the app, it succeeded.

or I replaced http://www.google.com with https://www.google.com, it also ran successfully in instant app.

Does instant app only support Https?


回答1:


That's right. HTTP connections are unsupported: https://developer.android.com/topic/instant-apps/policy.html#network_traffic.




回答2:


Add the below line into the manifest file

<application
    ...
    android:usesCleartextTraffic="true"
    ...
</application>


来源:https://stackoverflow.com/questions/44581900/instant-app-cleartext-communication-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!