2012-01-05 75 views
0

這裏是我的HTTP GET方法連接到的網址:爲什麼在這種情況下未知的主機?

public void executeHttpGet() throws Exception { 
    try { 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet("http://test.mobibon.com.tw/MovieGoTest/GetMovies.js"); 
     HttpResponse response = client.execute(request); 
     String retSrc = EntityUtils.toString(response.getEntity()); 

     Log.i("bird",retSrc); 
     } finally { 
     } 
    } 

這裏就是我把它叫做:

try { 
     executeHttpGet(); 
    } catch (Exception e) { 
     Log.i("bird","nothing"); 
     e.printStackTrace(); 
    } 

http://test.mobibon.com.tw/MovieGoTest/GetMovies.js < -----這個網址正常工作在我的瀏覽器

但是異常消息是:UnknownHostException

爲什麼?

回答

0

你好檢查這項功能

private InputStream retriveInputStream(String url){ 
    DefaultHttpClient client=new DefaultHttpClient(); 
    HttpGet getRequest=new HttpGet(url); 

    try{ 

     HttpResponse getResponse=client.execute(getRequest); 
     final int statusCode=getResponse.getStatusLine().getStatusCode(); 
     if(statusCode!=HttpStatus.SC_OK){ 
      Log.w(getClass().getSimpleName(),"Error " + statusCode + " for URL " + url); 
     } 
     HttpEntity getResponseEntity=getResponse.getEntity(); 
     return getResponseEntity.getContent(); 
    }catch(IOException ioe){ 
     ioe.printStackTrace(); 
    } 

    return null; 
} 

請參閱此鏈接JSON解析Android中Click Here

0

檢查你的XML允許存在與否: <uses-permission android:name="android.permission.INTERNET" />

+0

我之前已經添加此權限。 ... 但仍然感謝您的幫助! – bbbbbird1 2012-01-05 08:25:39

+0

在這裏,我也可以成功訪問url,嘗試重新構建並運行,或關閉所有網絡適配器並打開。 – idiottiger 2012-01-05 08:34:19

0

您的AVD可能沒有互聯網連接。

閉上你的AVD和CMD重新打開它作爲E:\android-sdk-windows\tools>emulator -avd mj -dns-server 8.8.8.8

相關問題