2010-09-12 55 views
9

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(); 
    } 
} 

回答

15

127.0.0.1是指在仿真器爲localhost,而不是你的機器。使用10.0.2.2連接到您的主機。

難道還要確保您所請求的INTERNET許可,您的AndroidManifest.xml

+0

感謝它的工作, – Troj 2010-09-12 17:04:13

+0

你知道它是如何可以讀取所發送的數據? – Troj 2010-09-12 17:04:32

+0

您可以在主機上使用WireShark來達到此目的。 – 2010-09-12 19:45:46

相關問題