2

I have a basic RESTful service implement in a VS 2015 project. I've just started learning Xamarin forms (in VS 2019) and I'm trying to verify that I can consume the service from the Android Emulator.

When I run the service in debug mode, I can use "http://localhost:49826/api/values/2" and get the data from the service successfully.

However, when I THEN use the same thing from my Xamarin project:

_client = new HttpClient(); _client.GetAsync("http://localhost:49826/api/values");

I get the following error:

{System.Net.WebException: Failed to connect to localhost/127.0.0.1:49826 ---> Java.Net.ConnectException: Failed to connect to localhost/127.0.0.1:49826

Following information I found here: https://learn.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services, I changed the call to:

_client.GetAsync("http://10.0.2.2:49826/api/values");

{Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

The client call works fine with "https://jsonplaceholder.typicode.com/posts", and the service works fine when invoked from the browser, but something is missing when I try to connect them. Any ideas?

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
Jeff Landon
  • 31
  • 1
  • 6
  • 1
    When using "Localhost" within Android, you are referring to the device|emulator itself, not your "host" (the PC) of the REST service. You can use the specially mapped IP address of 10.0.2.2 (that IP address within the Android emulator is mapped to its hosting PC) There are many SOs related to this, here is just one: https://stackoverflow.com/a/5806384/4984832 – SushiHangover May 10 '19 at 15:30
  • As indicated in the text of my question, I tried that. – Jeff Landon May 10 '19 at 16:07
  • You posted that you got a totally different error using 10.0.2.2, and you have different urls between what you say worked vs. what produces errors. How about editing the question and removing the references to localhost errors, and post the actual code, the return value and exception/stacktrace of your failed json parsing in order to make it clear what singular problem you are having. – SushiHangover May 10 '19 at 16:15
  • Nothing has worked. Both examples throw an exception, and I posted both results. I made a mistake with the first url, that included a "/2", but I get an exception no matter what I do. I have a breakpoint in the service, which does NOT get triggered from my Xamarin app, but does get triggered (and the service seems to work fine) when invoked from another browser. Sorry if I wasn't clear, I was trying to be concise. – Jeff Landon May 10 '19 at 16:26
  • It seems like you're able to hit the endpoint by using 10.0.2.2 since it's returning a response. What you can also do to test faster is browse to your endpoint using the android emulator's chrome or internet browser. If you still can't get a response it might be that your antivirus is blocking the traffic. – FernandoG May 10 '19 at 16:26
  • I remember what I did now. Hold on i'll post the steps. – FernandoG May 10 '19 at 16:30
  • Fernando, this is an excellent suggestion. I got an error "Bad Request - Invalid Hostname". This error page is probably wrapped in html, which would explain why I got that particular parser error. – Jeff Landon May 10 '19 at 16:31
  • @JeffLandon You are posting multiple problems in a single question, my first comment addresses your localhost problem, not your json parsing problem. Posting multiple problems in a single question will led to multiple answers, thus things can get confusing when someone is tryin to help. – SushiHangover May 10 '19 at 16:31
  • Thanks Sushi, Fernando has been helpful. – Jeff Landon May 10 '19 at 16:33
  • Answer given by Edward Brey in this post: https://stackoverflow.com/questions/6192726/android-emulator-loopback-to-iis-express-does-not-work-but-does-work-with-cassi -- did the trick for me. Thanks again to FernandoG for helping me get on the right track, and for not lecturing me about posting etiquette. – Jeff Landon May 10 '19 at 18:01

3 Answers3

2

I'm on Xamarin with Visual Studio and I needed to consume a REST-API at my dev machine from a physical Android device to follow a tutorial.

So I installed a free Visual Studio tool/plugin named Conveyor by Keyoti .

While your WebApp/Rest-Api is on Debug mode in Visual Studio at your development machine, Conveyor provides you 3 ways to connect to your applicacion from another device:

  • Local URL (a localhost URL, same as Visual Studio)
  • Remote URL (an URL to access your app from another device connected to your local network)
  • Internet URL (to access your app via Internet)

So I activated the internet URL option and successfully accessed my Rest-Api from my Android phone over my data plan (if your device is on the same local network go for Remote URL), but you'll have to sign up for a free accout in order to use the Internet URL option, which will remain for free several months further (according the website at the moment of writing this comment).

To install Conveyor in Visual Studio go to Tools >> Extensions and updates, then Online option at the left, search and install. You'll have to restart Visual Studio for installation to take effect.

Máster
  • 981
  • 11
  • 23
1

I had a similar problem and I resolved it like this :

To determine your IP Address go to cmd and type ipconfig. Choose your IPv4 address.

Draken
  • 3,134
  • 13
  • 34
  • 54
0

If you still have trouble hitting your local endpoint try switching from IIS Express to the full IIS available on windows. This has worked for me in the past. I know that IIS Express handles incoming traffic differently than IIS.

Windows: Start -> Search: Windows Features -> Turn Windows Features on or off -> Enable Internet Information Services.

At this point, navigate to localhost as make sure you see the IIS landing page.

Visual Studio (In Web Project)-> Project -> [ProjectName]Properties... -> Debug -> Switch Launch from IIS Express to IIS.

Keep in mind that if you do this switch to your project, and open this project in a computer that does not have IIS enabled, then they won't be able to open it. So you'll have to switch it back to IIS Express as needed.

FernandoG
  • 460
  • 5
  • 16