2011-11-15 102 views
1

我想從我的server.However網頁獲取數據時,我在httpClient.execute運行這段代碼它總是失敗()未知的主機異常

String flixURL=("http://myserver.com:6718/cgi-bin/log.pl?zip=" + zippy);    
Toast.makeText(getBaseContext(),flixURL,5).show(); 

DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpGet httpGet = new HttpGet(flixURL); 
ResponseHandler<String> resHandler = new BasicResponseHandler(); 
System.out.println("resHandler"+resHandler); 

try {     
String page = httpClient.execute(httpGet, resHandler);      
System.out.println("page"+page); 
} 
catch (ClientProtocolException e) 
{ 
e.printStackTrace();System.out.println(e); 
} 
catch (IOException e) { 
e.printStackTrace();System.out.println(e); 
} 

調試器告訴我可能是UnknownHostException。我嘗試過不同的網址,但問題仍然存在。

Android清單文件是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.andtwi" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:label="@string/app_name" 
      android:name=".AndTwitterActivity" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
       <activity android:name=".screen2" android:label="Screen2"> 
     </activity> 
     <uses-permission android:name="android.permission.INTERNET" /> 

    </application> 

</manifest> 

誰能給我暗示哪裏出了問題。謝謝..

+3

HTTP:/myserver.com:6718/cgi-bin目錄/ log.pl郵編= 你忘了'/'在URL的開頭 –

+0

@VitoShadow真的說了些什麼。您的網址不正確,因此爲UnknownHostException。 –

+0

@我更新了URL。這是正確的。在這裏複製時我被打亂了。 – typedefcoder2

回答

5

移動此行

<uses-permission android:name="android.permission.INTERNET" /> 

<application>標籤:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.andtwi" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:label="@string/app_name" 
      android:name=".AndTwitterActivity" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
       <activity android:name=".screen2" android:label="Screen2"> 
     </activity> 

    </application> 

</manifest> 
+0

Voila ...它工作...感謝LAS_Vegas ..嘿,你怎麼知道我的冬季度假目的地:) 順便說一句..你能解釋爲什麼你問我要移出那些代碼的代碼...謝謝 – typedefcoder2

+0

這是一個常見的錯誤,當它們在裏面時,權限不起作用:)有關詳細信息,請參閱http://developer.android.com/guide/topics/manifest/manifest-intro.html。 – Caner