Android应用程序连接到Web服务 - 不工作 [英] Android app connecting to a webservice - not working

查看:98
本文介绍了Android应用程序连接到Web服务 - 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IAM的尝试我的应用程序连接到我在asp.net中创建了一个WCF服务。 该服务运行在我LOCALMACHINE: 的http://本地主机:8080 / Service.svc /

但因为某些原因我的Andr​​oid无法连接到这个HTTP的联系地址。

这是错误:

09-12 14:50:44.540:WARN / System.err的(593):org.apache.http.conn.HttpHostConnectException:连接到 HTTP ://127.0.0.1:8080 拒绝
这是WCF的方法,荫试图返回一个集合了一些值。

  ///<返回>将(ID,项目)对的枚举。返回NULL,如果没有项目是present< /回报>
    保护覆盖的IEnumerable< KeyValuePair<字符串,SampleItem>> OnGetItems()
    {
        // TODO:在此处更改样本实现
        如果(items.Count == 0)
        {
            items.Add(A,新SampleItem(){值=A});
            items.Add(B,新SampleItem(){值=B});
            items.Add(C,新SampleItem(){值=C});
        }
        返回this.items;
    }
 


这是如何在android的连接是这样的:

 公共无效的getData(字符串URL)
{
    HttpClient的HttpClient的=新DefaultHttpClient();
    HTTPGET HTTPGET =新HTTPGET(URL);
    HTT presponse响应;

    尝试
    {
        响应= httpClient.execute(HTTPGET);
        Log.i(TAG,response.getStatusLine()的toString());
}赶上(ClientProtocolException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    } / *赶上(JSONException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    } * /赶上(例外五){
        e.printStackTrace();
    }最后{
        httpGet.abort();
    }
}
 

解决方案

127.0.0.1 是指在仿真器为localhost,而不是你的机器。

使用 10.0.2.2 来连接到你的主机。

难道还要确保您所要求的网​​络许可,您的Andr​​oidManifest.xml

Iam trying to connect my App to a WCF service that I created in asp.net. The service runs on my localmachine: http://localhost:8080/Service.svc/

But for some reasons my Android can not connect to this http-adress.

This is the error:

09-12 14:50:44.540: WARN/System.err(593): org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1:8080 refused
this is the method in wcf, Iam trying to return a collection with some values.

        /// <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;
    }


And this is how the connection in the android looks like:

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 refers to localhost in the Emulator, not your machine.

Use 10.0.2.2 to connect to your host machine.

Do also make sure you have requested the INTERNET permission in your AndroidManifest.xml

这篇关于Android应用程序连接到Web服务 - 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆