Android应用程序连接到web服务 - 不工作
问题描述:
Iam试图将我的应用程序连接到我在asp.net中创建的WCF服务。 服务在我的本地机器上运行: http://localhost:8080/Service.svc/Android应用程序连接到web服务 - 不工作
但由于某些原因,我的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;
}
这是在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
连接到您的主机。
难道还要确保您所请求的INTERNET
许可,您的AndroidManifest.xml
感谢它的工作, – Troj 2010-09-12 17:04:13
你知道它是如何可以读取所发送的数据? – Troj 2010-09-12 17:04:32
您可以在主机上使用WireShark来达到此目的。 – 2010-09-12 19:45:46