1

I am having a challenge with my Xamarin Forms Android app in Visual Studio 2015 Enterprise edition.

Below is my helper class which makes all calls to my WebAPIs

    public string CallWebService(string ps_URI)
    {
        HttpClient lobj_HTTPClient = null;
        HttpResponseMessage lobj_HTTPResponse = null;
        string ls_Response = "";
        string ls_Prefix = "";
        //We assume the internet is available. 
        try
        {
            ls_Prefix = Device.OnPlatform<string> (App.APISecurePrefix, App.APIPrefix, App.APIPrefix);
            //Get the Days of the Week
            //lobj_HTTPClient = new HttpClient(new NativeMessageHandler());
            lobj_HTTPClient = new HttpClient();
            lobj_HTTPClient.BaseAddress = new Uri(App.APISecurePrefix);
            lobj_HTTPClient.DefaultRequestHeaders.Accept.Clear();
            lobj_HTTPClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var lobj_Result = lobj_HTTPClient.GetAsync(ps_URI);

            while (!lobj_Result.IsCompleted)
            {
                Debug.WriteLine("WebAPICaller-CallWebService-1: URI:" + ps_URI);
                Task.Delay(100);
            }
            //GEt the http response object
            lobj_HTTPResponse = lobj_Result.Result;

            if (!lobj_HTTPResponse.IsSuccessStatusCode)
            {
                App.ProcessException(new Exception(lobj_HTTPResponse.ReasonPhrase));
            }
            else
            {
                var lobj_DataResult = lobj_HTTPResponse.Content.ReadAsStringAsync();

                while (!lobj_DataResult.IsCompleted)
                {
                    Debug.WriteLine("WebAPICaller-CallWebService-2");
                    Task.Delay(100);
                }
                ls_Response = lobj_DataResult.Result;
            }
        }
        catch (Exception ex)
        {
            App.ProcessException(ex);
        }
        finally
        {
            if (lobj_HTTPClient != null)
                lobj_HTTPClient.Dispose();
            if (lobj_HTTPResponse != null)
            {
                lobj_HTTPResponse.Dispose();
            }
            Debug.WriteLine("WebAPICaller-CallWebService-1: Done");
        }

        return ls_Response;

    }

It works fine on Windows phones but when I attempt to make WebAPI calls on the Android Emulator an exception is thrown as follows (See the TrustFailure):

Exception Information

Please note I am calling the WebAPI over a secure connection (https) and the server I am contacting does support both HTTP and HTTPS access to the WebAPIs.

I am using the latest stable version of Xamarin forms (2.3.3.180). I have removed the use of ModernHTTPClient. I did try to use the latest beta version of Xamarin Forms but then my needed components would not install so I went back to the last stable version.

Any idea how I can resolve this? If the solution involves doing anything with the Java or Android components outside of Visual Studio 2015, please provide specific details as I am not that familiar with how to do many Android / Java related things.

Cœur
  • 37,241
  • 25
  • 195
  • 267
George M Ceaser Jr
  • 1,497
  • 3
  • 23
  • 52
  • Can you provide the webApi you called? That can help us to try it on android platform. Dose the web api contains https. If yes please install the cer to your android device. – Mike Ma Feb 01 '17 at 01:52
  • So the problem was that I was still calling using https instead of http. I am going to update the question to how to call a secure web API on android. Why should I have to import the certificate to the phone. It is a fully trusted certificate issued to my production web API server. (www.cgsapi.com) – George M Ceaser Jr Feb 01 '17 at 14:07
  • eh~~ Can you provide the `HTTPClient.BaseAddress` and `ps_URI` for testing? – Mike Ma Feb 03 '17 at 07:28
  • 1
    Mike I am not sure why you need it. (i.e. I don't want to post it on here people to span it etc. ) As I said it works fine over HTTP and it works fine over HTTPS from the windows phone. – George M Ceaser Jr Feb 03 '17 at 16:33
  • You can put in any HTTPS webapi and receive the same error. If you have and you do not get the same error, please let me know. For example the following can be used to test and verify the error. https://www.cgsapi.com can be used to test along with ps_uri of /languages/true – George M Ceaser Jr Feb 03 '17 at 16:49
  • I have test url "https://www.cgsapi.com/languages/true" and get the `[{"Language_Code":"EN","Language_Name":"English"}]` with no exception. You can refer to the [github](https://github.com/mikexxma/Xamarin_WebAPI_Demo) – Mike Ma Feb 06 '17 at 08:50
  • I will check it out when I get home. Thanks – George M Ceaser Jr Feb 06 '17 at 15:09
  • After updating all the out of date components using Nuget - I cannot get the app to compile due to the following errors: D:\Visual Studio\VS 2015\POCProjects\Xamarin_WebAPI_Demo-master\XamarinWebApi\XamarinWebApi.Droid\Resources\values\styles.xml(2): error APT0000: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'. – George M Ceaser Jr Feb 06 '17 at 23:20
  • Also - your reference to System.Net.Http could not be found. I removed it and added a reference to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\WindowsPhoneApp\v8.1\System.Net.Http.dll – George M Ceaser Jr Feb 06 '17 at 23:26

0 Answers0