问题
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