andoird & libcurl - problems connecting to a localhost web service

Hi guys, I’ve managed to connect my app to a web service but I have a problem with the android platform.

Right now, the web service is hosted at “http://127.0.0.1:9876/ts?wsdl” at localhost.

I’ve managed to generate requests for it and it works great in the win32 app. The problem comes when I try to connect to the web service with the android app. It will always give “code: 7” (couldn’t connect) but the funny thing is that it will work if I connect to whatever web page.

So I wonder why it won’t let me connect to localhost, also note that I’m using the emulator since I don’t have a device.

Any ideas?

Check this http://stackoverflow.com/questions/13941048/libcurl-android-connecting-to-a-web-service-at-localhost

Seems that android maps your PC to ip 10.0.2.2, at least when running through emulator.
Or else find the local IP of your PC (probably 192.168.1.XX) and use that instead.

You could also try adding a line in etc/hosts to map localhost to a fake web address

127.0.0.1       some-website-name.com

Hi danadn _,

May i know how you managed to connect your app to a web service? I need help in that.
Any sample code or link will be great.
I never deal with web service before.

thanks,
-Smit

Hi smit, first of all I was asking for connecting to a webserver hosted on localhost. When accessing 127.0.0.1 from your app inside the emulator the localhost is, in fact, the emulator.

To let the emulator access the webserver hosted on you PC you have to use 10.0.2.2. So for example if you are trying two builds of your app, one for Win32 and one for Android, you can do this:

...
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) 
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:9876/ts?wsdl"); 
#endif 
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) 
curl_easy_setopt(curl, CURLOPT_URL, "http://10.0.2.2:9876/ts?wsdl"); 
#endif
...

Hope it helps.