开发者

Android app connecting to a webservice - not working

开发者 https://www.devze.com 2023-01-16 08:01 出处:网络
Iam trying to connect my App to a WCF service that I created in asp.net. The service runs on my localmachine:

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;
   开发者_如何学C }

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号