【发布时间】:2013-06-06 05:08:35
【问题描述】:
当我尝试执行以下代码时出现UnknownHostException...
String line;
String url = "http://mywebsite.com";
URL theURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection) theURL.openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
line = rd.readLine();
conn.disconnect();
我已按照以前帖子的建议确保 Internet 权限标记正确显示在清单中。我还确保我的 Android 具有 WIFI 访问权限,并且我尝试使用的网站是正确的。这通常似乎是 StackOverflow 上的热门资源:http://guerrarj.hubpages.com/hub/Tips-to-solve-the-UnknownHostException-on-Android
堆栈跟踪是...
java.net.UnknownHostException: the_website.com
at java.net.InetAddress.lookupHostByName(InetAddress.java:512)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:300)
at java.net.InetAddress.getAllByName(InetAddress.java:259)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
at com.example.privacyapp.MainActivity.scrapWeb(MainActivity.java:127)
at com.example.privacyapp.MainActivity.checkLocation(MainActivity.java:148)
at com.example.privacyapp.MainActivity$1.onLocationChanged(MainActivity.java:93)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4263)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
更新:我的 WIFI 连接似乎有问题。如果没有连接,就会出现 UnknownHostException。
【问题讨论】:
-
你是否在清单中添加了互联网权限?
-
是的,我确实这样做了——您指的是不是我在原始帖子中提到的内容?
-
intenetn 权限并检查您在移动浏览器上的链接是否在移动浏览器上工作
-
您的设备是否正常连接到互联网?正如 @PintuCorna 所述,您能否在设备的移动浏览器中打开该链接?
-
当我尝试测试 myapp.herokuapp.com 时,我也得到了 unknownhost.. 所以那个网站有问题
标签: android android-emulator android-wifi