2011-09-25 74 views
1

我有这个简单的代码,这使得到一个web服务。 出于某种原因,无法连接,并给出了日志的UnknownHostExceptionUnknownHostException(web服务http-get)

此错误是代码:

String URL = "http://services.sapo.pt/EPG/GetChannelList"; 
String result = ""; 
final String tag = "Data received: "; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final Button btnSearch = (Button)findViewById(R.id.btnSearch); 
    btnSearch.setOnClickListener(new Button.OnClickListener(){ 
     public void onClick(View v) { 
      callWebService(); 
     } 
    }); 

} // end onCreate() 

public void callWebService(){ 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpGet request = new HttpGet(URL); 
    ResponseHandler<String> handler = new BasicResponseHandler(); 

    try { 
     result = httpclient.execute(request, handler); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    httpclient.getConnectionManager().shutdown(); 
    Log.i(tag, result); 
} 

这是我表现

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

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".AndroidApp" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 
+0

检查您的网址是否正常工作,可能首先使用浏览器。 –

+0

是啊...我想你可能没有在您的浏览器连接 –

+0

网址工作,检查浏览器,它已经上网了。它是用v1.6创建的。我在网上搜索,这一切都指向增加许可证清单,但我已经做到了。 – bruno

回答

1

我找到了解决方案。 我的推杆使用GUI在清单里面添加标签Internet权限

<许可安卓名“android.permission.INTERNET对” />

然后用手,我的推杆正确的标签是:

<用途的许可机器人:名称= “android.permission.INTERNET对”/>

这是一个不同的标签和它的工作。 GUI失败。

0

不知道一部分,如果你已经检查了这一点,但请检查您是否在代理或防火墙后面。这里是way to configure the HttpClient for a proxy和官方apache documentation

+0

在这个网络中没有使用代理服务器,我正在使用其他应用程序和其他应用程序的Web服务,他们的工作。这个由于某种原因不是。 – bruno