如何解决模拟器错误-com.android.volley.NoConnectionError:java.net.UnknownHostException:无法解析主机
我有一个基本的Volley请求,每次在模拟器上尝试运行该应用程序时,都会引发此错误,但是在真实设备上运行良好,我向清单中添加了“ network_security_config”,但仍然收到错误。
private static final String url = "https://pixabay.com/api/?key=5303976-fd6581ad4ac165d1b75cc15b3&q=kitten&image_type=photo&pretty=true";
Cache cache = new DiskBasedCache(getCacheDir(),1024 *1024);
Network network = new BasicNetwork(new HurlStack());
mRequestQueue = new RequestQueue(cache,network);
mRequestQueue.start();
StringRequest request = new StringRequest(url,new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG,"Response: " + response);
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
network_security_config
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
清单
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:largeHeap="true"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。