我正在尝试将我的Android应用程序连接到网站。我使用的代码在Java中工作,所以这不是问题。我试过在这里寻找解决方案,但似乎没有任何效果。我试过了:
1)向清单添加互联网权限
2)将代码放在新线程上,因为主UI线程上显然不允许网络连接。
3)试图摆弄模拟器上的wifi选项
我不可避免地遇到异常:“引起:android.system.GaiException:android_getaddrinfo失败:EAI_NODATA(没有与主机名关联的地址)”
这是我的代码段:
private void getPriceFromWeb() {
final String priceStr;
double priceDouble;
Document website = null;
try {
website = Jsoup.connect("https://blockchain.info/ticker").ignoreContentType(true).get();
} catch (Exception e) {
e.printStackTrace();
System.out.println("EXCEPTION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
我现在不知所措。欢迎任何建议。再一次,如果我将它作为普通的java程序运行,实际的代码可以工作,但是不能用作android应用程序。这非常令人沮丧。
这是堆栈跟踪:
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/System.err: java.net.UnknownHostException: Unable to resolve host "blockchain.info": No address associated with hostname
W/System.err: at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:125)
W/System.err: at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:752)
W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
W/System.err: at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:652)
W/System.err: at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:629)
W/System.err: at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:261)
W/System.err: at org.jsoup.helper.HttpConnection.get(HttpConnection.java:250)
W/System.err: at com.example.Unutuku.paymenrequestqr.QRrequest.getPriceFromWeb(QRrequest.java:59)
W/System.err: at com.example.Unutuku.paymenrequestqr.QRrequest.access$000(QRrequest.java:19)
W/System.err: at com.example.Unutuku.paymenrequestqr.QRrequest$2.run(QRrequest.java:47)
W/System.err: at java.lang.Thread.run(Thread.java:761)
W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
W/System.err: at libcore.io.Posix.android_getaddrinfo(Native Method)
W/System.err: at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
W/System.err: at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:106)
W/System.err: ... 21 more
I/System.out: EXCEPTION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!