Android应用程序连接到Web服务 - 不工作连接到、应用程序、工作、Android

由网友(自信的女孩最美丽)分享简介:IAM的尝试我的应用程序连接到我在asp.net中创建了一个WCF服务。该服务运行在我LOCALMACHINE:的http://本地主机:8080 / Service.svc / 但因为某些原因我的Andr​​oid无法连接到这个HTTP的联系地址。这是错误:09-12 14:50:44.540:WARN /...

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();
    }
}
 
Android安卓开发环境搭建详细教程

解决方案

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

阅读全文

相关推荐

最新文章