Lizzard
Lizzard

Reputation: 193

Calling a REST Webservice from Android Emulator

I am trying to call a REST webservice from an Android app that I am running on an emulator. I can call the service from my browser with

http://localhost:39064/AndroidServiceImpl.svc/submitexcerpt?excerpt=...and+the+pirate+fell+off+the+boat

I can call the service from my emulator with

http://10.0.2.2:39064/AndroidServiceImpl.svc/submitexcerpt?excerpt=...and+the+pirate+fell+off+the+boat

But when I run this code:

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response1;
response1 = httpclient.execute(new HttpGet("http://10.0.2.2:39064/AndroidServiceImpl.svc/submitexcerpt?excerpt=testingthisout"));

I get a connection refused exception. I have added the <user-permission android:name="android.permission.INTERNET"></user-permission> to my Manifest file, and set the ThreadPolicy to LAX but still no luck. Any Suggestions?

Upvotes: 2

Views: 7470

Answers (2)

Lizzard
Lizzard

Reputation: 193

I got the answer rather than putting <user-permission android:name="android.permission.INTERNET"></user-permission>
I needed
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

So just some bullet points on calling a webservice:
*add to Manifest.xml file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
*use 10.0.2.2 rather than localhost for a locally consumed service
*Set the Thread Policy to lax(not recommended for production) or use AsyncTask
ThreadPolicy tp = ThreadPolicy.LAX; StrictMode.setThreadPolicy(tp);

Upvotes: 2

Mouna Cheikhna
Mouna Cheikhna

Reputation: 39638

Verify that 10.0.2.2 is your machine address(run ifconfig to verify it ), also this is a common problem, when there is no internet on the emulator so make sure your emulator is connected.

Upvotes: 0

Related Questions