Actuall working sample of Xamarin Form that does REST on Android

Can someone please provide an actual working sample of a Xamarin Forms project that also works with Android? We tried to use the TODORest Sample which initially wouldn't even build but after having downloaded the latest version from GitHub we at least got it to work. But communicate with the REST API just does not seem to work on Android (but does on UWP). There are numerous threads here and on other forums about similar problems with HttpClient, RestSharp etc.

Posts

  • improwiseimprowise Member ✭✭

    No one?

  • AYaegerAYaeger Member ✭✭

    I've been successfully using HttpClient in Xamarin.Forms and it seems to be fine on both iOS and Android. What is the issue that you are seeing when attempting to use it? There are so many reasons you might have issues with a REST API (as I'm sure you are aware) - many of them outside of the app itself. It could be some sort of environment set up issue or something on the web API side, and not something you are doing wrong in Xamarin.Forms with the HttpClient - especially since you are saying that it's working on UWP.

    Here's a good guide to get started with REST API using HttpClient on Xamarin.Forms that should work with all platforms (sorry I'm new to the forums so I can't post the actual full link due to restrictions for new users).

    docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/consuming/rest

  • improwiseimprowise Member ✭✭

    Thanks for responding. How have you verified this working? Which version of Visual Studio did you use and what version of Xamarin?

    For us, not even Xamarins own TODORest example works on Android. It works fine on Windows 10 though, as well as our own app, so our REST implementation in itself should work fine. Just not on Android. As we are mainly evaluating right now, we have not bothered to arrange for a Mac in the network so we have not been able to try it on iOS.

    We are using Visual Studio 2017 15.6.2 (was 15.5 when we started) and Xamarin components should be the latest available. We have tried this both using the Android Emulator and the Live Player on 2 different Androids. What happens is that the HTTP calls just don't seem to do anything. We know some exception are thrown as we land in our catch() but the exception in itself is always null. We have never experienced anything like this and my feeling is that it is an "environment related problem" rather than the code in itself being wrong. We also made sure the Android project has "INTERNET" in the Android Manifest but that does not seem to chang anything.

    We can also see in our debugger that the objects, before the exception is thrown, does not look like they normally do in "normal .NET". This is probably related to something specific witn Android but my knowledge about Android is not all that great, from a developers perspective. We also freshly installed a new computer just to try this out, and used the Xamarins TODORest sample, but it has the same problem.

    I have seen several other posts about HTTPClient with Xamarin and Android. What would be the most common errors an experienced .NET developer could do that could cause this when starting out with Xamarin Forms? But regardless of that, you would at least expect Xamarins own sample app to work but it does not.

  • NMackayNMackay GBInsider, University mod
    edited March 2018

    I've developed multiple apps in use daily use HTTPClient and Restful services as have thousands of people here.

    Before you post try and asertain a few facts:

  • improwiseimprowise Member ✭✭
    edited March 2018

    @NMackay said:
    I've developed multiple apps in use daily use HTTPClient and Restful services as have thousands of people here.

    Before you post try and asertain a few facts:

    Thanks for responding.

    Wish I could say that I haven't tried something but have tried everything I can think of and still no go.

    1. Yes.
    2. No, not for this tesing
    3. Yes
    4. No.
    5. See 2

    My suspicion is that this is related to configuration or bugs in Xamarin, either in the emulator/Live player or the platform itself. I mean, not even Xamarins own REST sample work on any of the machines we have tried it on for Android. For Windows (UWP) they work without any problem at all, so the REST code in itself should be working. The same goes for our evaluation app, works perfectly with UWP. What I have read, we are not the first to have problems with Xamarin Forms and Android.

    But I really want this to work but when not even Xamarins own samples does, its easy to loose faith...

    Are you using Xamarin Forms BTW or "native" Xamarin? Haven't tried the later yet but will as I've run out of ideas to try now.

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    Did you only tried it on an Android Emulator? If so then get a real device and test it again

  • improwiseimprowise Member ✭✭
    edited March 2018

    Well, the Android Emulator crashed almost instantly and was nowhere to be seen after that. That also seem to be a quite common problem. Just got a comment from David B on GitHub though that the Live Player does not support JSON.NET as it does not support reflection. Perhaps that would have been a good thing to mention when Visual Studio constantly nags about it being the future and encouraging you to use that instead. Have now installed the Visual Studio Android emulator and will try with that one. Does this mean that REST (etc.) is a no go using the Live Player?

    As for trying out on a real device, and not using the Live Player then, I guess you would need to build an APK then? Which does not seem to be possible without going through the entire process of signing etc or am I wrong?

  • improwiseimprowise Member ✭✭
    edited March 2018

    Well, guess what, when giving up on Live Player and throwing out the android emulator that gets installed by VS2017 by default and instead using the Visual Studio Emulator (which ironically isn't the one installed by VS2017) I finally was able to get REST to work. At least with the TODORest sample.

    Unbelievable, now I really understand people saying that Xamarin is great if you manage to get it to work but it is a nightmare getting there with configuration problems, horrible emulators, updates that breaks things that used to work, lack of documentation etc. Really hope that this was the end of the worst nightmare...

    Edit:
    BTW, I had to try to start the app on the Visual Studio Emulator about 10 times before it actually managed to start. The first time it just failed, despite being the exact same code all attempts. So don't give up when an app refuses to start the first 9 times...

  • improwiseimprowise Member ✭✭

    And for anyone having problems with being able to run the app in the Visual Studio Emulator for Android but not debugging, I recommend trying out this:

    http://dotnetbyexample.blogspot.se/2016/02/fix-for-could-not-connect-to-debugger.html

    We finally have our app working with REST on Android and being possible to debug it.

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    @improwise Good that it's finally working for you. But as a friendly reminder to save you some sanity. Do not trust or use the emulators^^ Neither Android nor iOS. Both are crap. I was so much faster, after I switched to real devices.

    And something more on the side of REST. If your API implements TLS1.2 you should switch from HttpClient to ModernHttpClient. Mono doens't support TLS1.2 currently

  • improwiseimprowise Member ✭✭

    @RaphaelSchindler and others, thanks for the help so far. Will look into the ModernHttpClient also, although we could prefer to use RestSharp if possible and the not the HttpClient directly as we use that for other implementations.

  • CameronCameron USMember ✭✭

    The original modernhttpclient is an out of date dead project. Use the updated one from alexrainman or just use the native implementations, which can be configured in the project settings.

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    @Cameron You mean this one I guess https://github.com/alexrainman/ModernHttpClient

    Would be nice of you to post a link the next time :smile:

  • CameronCameron USMember ✭✭

    That's it, I can't post links.

Sign In or Register to comment.