async/await method crash web services response in android but same code work successfully in other

async/await method crash web services response in android but same code work successfully in window Phone and iOS apps in xamarin Native Appliation.

Xamarin Native Application (PCL)

I have created a Native application using portable Class libarary.

I request to REST API that gives the following response.

    {
    code: 200,
    status: "ok",
    message: "hello"
    }

my Portable class library code in xamarin.forms is

test.xaml.cs

    public class jsonResponseClass
    {
            public string code { get; set; }
            public string status { get; set; }
            public string message { get; set; }
    }
    public partial class test : ContentPage
    {
            public async void getDatas()
            {
                var cl = new HttpClient();
                var result = await cl.GetStringAsync("http://192.168.1.125/apps/jara/web/api/user/test");
                jsonResponseClass des = JsonConvert.DeserializeObject<jsonResponseClass>(result);
                lbl1.Text = des.code + " " + des.status + " " + des.message;
            }
            public test()
            {
                InitializeComponent();
                getDatas();
            }
    }

Main file in Portable class library (PCL)

App.cs

namespace NativeAppsWithWCF
{
    public class App : Application
    {
        public App()
        {
            MainPage = new test();
        }
        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

my visual studio solution explorer is

Using this above code I can successfully show output in iOS and Window Phone Apps.

But when i Run Android apps then crash the apps and not show output.

When i debug android program then cursor remove after this line.

var result = await
cl.GetStringAsync("http://192.168.1.125/apps/jara/web/api/user/test");

After this line i got an error and stop the execution of program.

See the screen after got error..

How to prevent this error ?

Answers

  • AdamPAdamP AUUniversity ✭✭✭✭✭
    edited November 2015

    @ShahabuddinVansiwala - check the output window or Android Device Logging (logcat) messages to see the issues on Android devices. I would probably have a guess that you might not have set the Internet permission in your Android app but without the errors I couldn't tell you.

    https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/android_debug_log/ - if you didn't know about the android logging.

  • ShahabuddinVansiwalaShahabuddinVansiwala USMember ✭✭
    edited November 2015

    @Adamp Already i have given the permission of Internet.

    the main problem is after below line .... the execution has stopped and exit the current method.

    There are no give any exception or error message.

    var result = await
    cl.GetStringAsync("http://192.168.1.125/apps/jara/web/api/user/test");

    This problem is only in android. I have successfully run iOS and Window phone project with same coding.

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @ShahabuddinVansiwala - please provide the logcat output so we can see what Android errors are occurring. That will give more information.

  • ShahabuddinVansiwalaShahabuddinVansiwala USMember ✭✭
    edited November 2015

    @AdamP

    Finally I have solved the problem after lots of R&D.

    Problem in Network Connectivity.

    I have solve this issue after setting of Network Connectivity in Android Emulator.

    Follow Below step..

    Open VS Android Emulator >> Go to Setting >> Turn ON WIFI >>

    There are "Wired eth1" Network available.

    Long Press on "Wired eth1" Network

    Then Click on >> Modify Network >> Change "IP Setting" as "Static" >>

    Change IP address of your Network.

    After this setting I have Solved my Issue and Get a Response from REST API.

    Thank you.

  • sqltsqlt USMember
    edited May 2016

    @ShahabuddinVansiwala
    Thanks so much for your reply. That was exactly my problem. Spent over an hour trying to solve that. The only thing I'd add is that I didn't have to change the IP address, just changing the IP Setting to static and saving with the default IP it provided worked for me.

  • AndreLuiz.1614AndreLuiz.1614 USMember

    I'm having the same problem, however I'm running the app on the physical phone by the usb.

    The error happens exactly in the httpClient.GetAsync () call;

    Already i have given the permission of Internet.

    Error:
    Time Device Name Type PID Tag Message
    03-28 08:14:37.907 Sony D6643 Error 28667 mono-rt [ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: SecureChannelFailure (The authentication or decryption has failed.) ---> System.IO.IOException: The authentication or decryption has failed. ---> System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: The authentication or decryption has failed.
    at Mono.Security.Protocol.Tls.RecordProtocol.EndReceiveRecord (System.IAsyncResult asyncResult) [0x0003a] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:430
    at Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (System.IAsyncResult ar, System.Boolean ignoreEmpty) [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs:256
    at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (System.IAsyncResult result) [0x00071] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs:418
    --- End of inner exception stack trace ---
    at Mono.Security.Protocol.Tls.SslClientStream.EndNegotiateHandshake (System.IAsyncResult result) [0x00035] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs:396
    at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (System.IAsyncResult asyncResult) [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs:101
    --- End of inner exception stack trace ---
    at Mono.Security.Protocol.Tls.SslStreamBase.EndRead (System.IAsyncResult asyncResult) [0x00051] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs:883
    at Mono.Net.Security.Private.LegacySslStream.EndAuthenticateAsClient (System.IAsyncResult asyncResult) [0x00011] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/System/Mono.Net.Security/LegacySslStream.cs:475
    at Mono.Net.Security.Private.LegacySslStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/System/Mono.Net.Security/LegacySslStream.cs:445
    at Mono.Net.Security.MonoTlsStream.CreateStream (System.Byte[] buffer) [0x0004e] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/System/Mono.Net.Security/MonoTlsStream.cs:105
    --- End of inner exception stack trace ---
    at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x0005e] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1029
    at System.Threading.Tasks.TaskFactory1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func2[T,TResult] endFunction, System.Action1[T] endAction, System.Threading.Tasks.Task1[TResult] promise, System.Boolean requiresSynchronization) [0x00014] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:550

    Thanks.

  • AndreLuiz.1614AndreLuiz.1614 USMember

    I put an exception and gave this error...

  • AndreLuiz.1614AndreLuiz.1614 USMember

    I solved my problem too, if anyone needs to follow the solution:

    This is quite an old post, but for people still getting this, know that the answer of EdHubbel is correct.

    If you encounter this issue using the HttpClient class like this HttpClient client = new HttpClient();

    then install the "modernhttpclient" nuget package to your project (both PCL, and native projects), and then change your HttpClient class initialization to this:

    HttpClient client = new HttpClient(new NativeMessageHandler());

    The NativeMessageHandler class will handle for you these certificates issues

    reference: https://forums.xamarin.com/discussion/10405/the-authentication-or-decryption-has-failed-in-the-web-request

    Thanks!

  • himel951himel951 Member
    edited April 2018

    Lot of tnx AndreLuiz.1614. I just followed your instruction plus add the Internet Access in the android manifest. now HttpClient works without any error. :)

Sign In or Register to comment.