c# - 连接到网络服务的 Android 应用程序 - 无法正常工作

标签 c# java android wcf

我正在尝试将我的应用程序连接到我在 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/

相关文章:

c# - 更改 AppSetting 对应用程序没有影响

android - Android 设备中的 Flash 动画

android - 如何在android中调试wpa_supplicant?

C#随机数

c# - JIT 编译后如何(以及在​​哪里)加载 native 代码?

java - 如何在java类中嵌入版本字符串

java - Netty 服务器中 Dropwizard 指标和 JMeter 之间的延迟值不一致

java - JBoss 6 EAP,如何更改模块加载顺序?

java - 循环动画可绘制

c# - 在 C# 堆栈溢出中使用访问器