在Android中使用HTTPConnection的UnknownHostException

时间:2013-06-06 05:08:35

标签: android android-emulator android-wifi

当我尝试执行以下代码时出现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();

我已按照以前的帖子提示确保在清单中正确显示互联网权限标记。我还确保我的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。

1 个答案:

答案 0 :(得分:0)

有时我们必须写ip地址而不是“http://mywebsite.com”;所以尝试使用IP地址。 check check INTERNET权限标记是清单标记的子标记,而不是应用程序标记。 并检查this once希望这对您有帮助