ModernHttpClient not throwing Java.Net Exceptions to Android project

Sreeraj.0276Sreeraj.0276 SreerajUSMember ✭✭
edited June 2015 in Android

I am using ModernHttpClient for my APIs and I am using client.PostAsync() inside try-catch block. When I turned off internet and tried I was expecting to get a callback to my catch block. Rather App exited with an exception. It seems like the exception is thrown in Library PCL but it is not caught there.

This is my code

try
{
        using (var response = await client.PostAsync(new Uri (baseUrl + "/api/Account/Prelogin"), content)) {
        using (var responseContent = response.Content) {
            result = await responseContent.ReadAsStringAsync();
            return result;
        }
    }
}
catch (Exception ex)
{
    return "Connection issue";
}

Here is the stack trace

--- End of managed exception stack trace ---
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    ... 1 more
Caused by: java.net.UnknownHostException: Unable to resolve host"xxxxxxxxxxxxxxxxxxxxxx": No address associated with hostname
    at java.net.InetAddress.lookupHostByName(InetAddress.java:457)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
    at java.net.InetAddress.getAllByName(InetAddress.java:215)
    at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
    at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:224)
    at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:193)
    at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:113)
    at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:344)
    at com.squareup.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:329)
    at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:319)
    at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
    at com.squareup.okhttp.Call.getResponse(Call.java:271)
    at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
    at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
    at com.squareup.okhttp.Call.access$100(Call.java:34)
    at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:162)
    at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)
Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
    at libcore.io.Posix.android_getaddrinfo(Native Method)
    at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
    ... 19 more

Posts

  • DimitarIvanovDimitarIvanov Dimitar Ivanov USMember ✭✭

    @SreejaVS.6303
    Caused by: java.net.UnknownHostException: Unable to resolve host"xxxxxxxxxxxxxxxxxxxxxx": No address associated with hostname

    Can you access the host from outside than your application ot your local system.
    It seams that the system can't access the host,did you enable all the permissions and so on ?

  • Sreeraj.0276Sreeraj.0276 Sreeraj USMember ✭✭

    Yes. I can access. In fact, I can even access it from the application. This error occurs only when I turn off internet. I just want to catch all these exceptions. My issue is that, rather than the control being transfered to catch block(in my Android project), the exception is thrown in library project and my application is crashing.

    Yes I have set all the permissions correctly.

  • Paul_RoyPaul_Roy Paul Roy FRMember

    Hi,
    Did you fix this issue ? I'm experiencing this and it is quite anoying, as one of our french providers (named Free) keeps interrupting its service, causing a LOT of these exceptions (several thousands).

  • Paul_RoyPaul_Roy Paul Roy FRMember

    Hi,
    Did you fix this issue ? I'm experiencing this and it is quite anoying, as one of our french providers (named Free) keeps interrupting its service, causing a LOT of these exceptions (several thousands).

  • LouisHaussknechtLouisHaussknecht Louis Haußknecht DEMember
    edited September 2016

    We have the same issue, but a little different. The first call when not connected throws a UnknownHostException and can be catched. Now turning on the network an submitting the next call causes the app to crash:

    09-29 16:53:42.741: W/dalvikvm(14941): Invalid indirect reference 0xa2590e20 in decodeIndirectRef
    09-29 16:53:42.741: I/dalvikvm(14941): "OkHttp https://*****/token" prio=5 tid=14 RUNNABLE
    09-29 16:53:42.741: I/dalvikvm(14941):   | group="main" sCount=0 dsCount=0 obj=0xb30f97f8 self=0xb8ff6130
    09-29 16:53:42.741: I/dalvikvm(14941):   | sysTid=15173 nice=0 sched=0/0 cgrp=apps handle=-1179120768
    09-29 16:53:42.741: I/dalvikvm(14941):   | state=R schedstat=( 49673122 22762814 2813 ) utm=4 stm=0 core=0
    09-29 16:53:42.741: I/dalvikvm(14941):   at md595f4b8ab717b9da13f11267a86077f20.AwaitableOkHttp_OkTaskCallback.monodroidAddReference(AwaitableOkHttp_OkTaskCallback.java:-1)
    09-29 16:53:42.741: I/dalvikvm(14941):   at md595f4b8ab717b9da13f11267a86077f20.AwaitableOkHttp_OkTaskCallback.n_onResponse(Native Method)
    09-29 16:53:42.741: I/dalvikvm(14941):   at md595f4b8ab717b9da13f11267a86077f20.AwaitableOkHttp_OkTaskCallback.onResponse(AwaitableOkHttp_OkTaskCallback.java:39)
    09-29 16:53:42.741: I/dalvikvm(14941):   at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
    09-29 16:53:42.741: I/dalvikvm(14941):   at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
    09-29 16:53:42.741: I/dalvikvm(14941):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    09-29 16:53:42.741: I/dalvikvm(14941):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    09-29 16:53:42.741: I/dalvikvm(14941):   at java.lang.Thread.run(Thread.java:841)
    09-29 16:53:42.741: E/dalvikvm(14941): VM aborting
    09-29 16:53:42.741: A/libc(14941): Fatal signal 6 (SIGABRT) at 0x00003a5d (code=-6), thread 15173 (OkHttp Dispatch)
  • ChrisCarr.9577ChrisCarr.9577 Chris Carr USMember

    Similar issue here - Java.Net.UnknownHostException when simulating 100% network loss - have found no solution, yet.

    Java.Net.UnknownHostException: Unable to resolve host "data.********.com": No address associated with hostname ---> Java.Lang.RuntimeException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
      at android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
      at at libcore.io.Posix.android_getaddrinfo(Native Method)
      at at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
      at at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
      at at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
      at at java.net.InetAddress.getAllByName(InetAddress.java:215)
      at at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
      at at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:224)
      at at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:193)
      at at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:113)
      at at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:344)
      at at com.squareup.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:329)
      at at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:319)
      at at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
      at at com.squareup.okhttp.Call.getResponse(Call.java:271)
      at at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
      at at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
      at at com.squareup.okhttp.Call.access$100(Call.java:34)
      at at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:162)
      at at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
      at at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
      at at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
      at at java.lang.Thread.run(Thread.java:818)
    
  • OsamaRabeaOsamaRabea Osama Rabea USMember

    In case anyone is still trying to figure this out. It seems to be an issue with ModernHttpClient. I've started using Xamarin.Android.Net.AndroidClientHandler instead of NativeMessageHandler in my Android implementation and this resolved the issue.

  • BerayBentesenBerayBentesen Beray Bentesen TRUniversity ✭✭✭✭

    ModernHttpClient is useless since Xamarin supports native http client

  • BBrightBBright Bright Lee KRMember ✭✭✭

    @BerayBentesen

    How to use Xamarin's version of native http client?

    Just using "client = new HttpClient();" is OK then?

  • BerayBentesenBerayBentesen Beray Bentesen TRUniversity ✭✭✭✭

    @BBright Please check the end of post for detailed example.

Sign In or Register to comment.