我正在尝试将我的应用程序连接到我在 asp.net 中创建的 WCF 服务。 该服务在我的本地计算机上运行: http://localhost:8080/Service.svc/
但由于某些原因,我的 Android 无法连接到此 http 地址。
这是错误:
09-12 14:50:44.540: WARN/System.err(593): org.apache.http.conn.HttpHostConnectException: 连接到 http://127.0.0.1:8080拒绝
这是 wcf 中的方法,我试图返回一个包含一些值的集合。
/// <returns>An enumeration of the (id, item) pairs. Returns null if no items are present</returns>
protected override IEnumerable<KeyValuePair<string, SampleItem>> OnGetItems()
{
// TODO: Change the sample implementation here
if (items.Count == 0)
{
items.Add("A", new SampleItem() { Value = "A" });
items.Add("B", new SampleItem() { Value = "B" });
items.Add("C", new SampleItem() { Value = "C" });
}
return this.items;
}
这就是 android 中的连接方式:
public void getData(String url)
{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response;
try
{
response = httpClient.execute(httpGet);
Log.i(TAG,response.getStatusLine().toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}/* catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */catch (Exception e){
e.printStackTrace();
}finally{
httpGet.abort();
}
}
最佳答案
127.0.0.1
是指模拟器中的本地主机,而不是您的机器。
使用 10.0.2.2
连接到您的主机。
还要确保您已在 AndroidManifest.xml 中请求了 INTERNET
权限
关于c# - 连接到网络服务的 Android 应用程序 - 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3695587/