Android 应用程序连接到网络服务 - 不工作

2024-01-01

我正在尝试将我的应用程序连接到我在 asp.net 中创建的 WCF 服务。 该服务在我的本地计算机上运行:http://localhost:8080/Service.svc/ 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 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指模拟器中的本地主机,而不是您的计算机。

Use 10.0.2.2连接到您的主机。

还要确保您已请求INTERNETAndroidManifest.xml 中的权限

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 应用程序连接到网络服务 - 不工作 的相关文章

随机推荐