我试图将我的应用程序连接到我在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被拒绝
这是在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; }
这就是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是指仿真器中的localhost,而不是您的机器.
使用10.0.2.2连接到您的主机.
还请确保您已经在AndroidManifest.xml中请求了INTERNET权限