Android应用程序连接到Web服务

编程入门 行业动态 更新时间:2024-10-27 20:33:39
本文介绍了Android应用程序连接到Web服务 - 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

IAM的尝试我的应用程序连接到我在asp中创建了一个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. The service runs on my localmachine: 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 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服务

本文发布于:2023-11-01 17:30:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   应用程序   Android   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!