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

Tro*_*roj 9 c# java wcf android

我试图将我的应用程序连接到我在asp.net中创建的WCF服务.该服务在我的localmachine上运行: http:// localhost:8080/Service.svc /

但由于某些原因,我的Android无法连接到此http-adress.

这是错误:

09-12 14:50:44.540:WARN/System.err(593):org.apache.http.conn.HttpHostConnectException:连接到http://127.0.0.1:8080拒绝
这是wcf中的方法,Iam试图返回带有一些值的集合.

        /// <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;
    }
Run Code Online (Sandbox Code Playgroud)


这就是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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*een 15

127.0.0.1 是指模拟器中的localhost,而不是您的机器.

使用10.0.2.2连接到您的主机.

还要确保您已INTERNET在AndroidManifest.xml中请求了权限