user6310522
user6310522

Reputation: 51

Instant app CLEARTEXT communication not supported: []

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?

Upvotes: 2

Views: 4784

Answers (2)

Dino Sunny
Dino Sunny

Reputation: 1051

Add the below line into the manifest file

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

Upvotes: 2

philo
philo

Reputation: 3690

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

Upvotes: 4

Related Questions