我有一段简单的代码,可以让get访问get服务。由于某种原因,它无法连接,并在日志unknownHostException中显示错误
代码如下:
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>发布于 2011-10-04 22:53:04
我找到了解决方案。我使用添加了标签的manifest中的GUI输入internet权限
<权限android:name"android.permission.INTERNET"/>
然后,我手动输入正确的标签,即:
发布于 2011-09-25 17:42:50
不确定您是否已经检查过此选项,但请检查您是否在代理服务器或防火墙之后。这是一个way to configure the HttpClient for a proxy和官方apache documentation。
https://stackoverflow.com/questions/7544654
复制相似问题