2013-06-06 51 views
0

UnknownHostException出現當我嘗試執行以下代碼的HttpConnection ...的UnknownHostException在Android的

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訪問,並且我嘗試使用的網站是正確的。這通常似乎是在計算器上的熱門資源: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

是的,我做過了 - 你指的是我在原始文章中提到的內容嗎? – NumenorForLife

+0

intenetn授權,並檢查您的手機瀏覽器鏈接它是否在移動瀏覽器上工作 – CoronaPintu

回答

0

有時我們必須寫ip地址而不是「http://mywebsite.com」;所以請嘗試使用IP地址。 還請檢查INTERNET權限標記是清單標記的子項,而不是應用程序標記。 和支票this once希望這對您有所幫助