Problem with an API request

Joao Carlos Del Vecchio 41 Reputation points
2022-10-24T08:08:17.393+00:00

Greetings,
I have an API call that works often, but sometimes I get back an error I logged and it is this:

JniPeerMembers : Android.Runtime.XAPeerMembers

StackTrace : at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <11eeb492016f4a8c999585f0490be620>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0003c] in <11eeb492016f4a8c999585f0490be620>:0
at Java.Net.HttpURLConnection.get_ResponseCode () [0x0000a] in <81c1d29afadd437db7a7f9c0ac3aa46f>:0
at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass46_0.<DoProcessRequest>b__2 () [0x00000] in <81c1d29afadd437db7a7f9c0ac3aa46f>:0
at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <ca06dc8ec1b243b7afbe4f88994c9098>:0
at System.Threading.Tasks.Task.Execute () [0x00000] in <ca06dc8ec1b243b7afbe4f88994c9098>:0
--- End of stack trace from previous location where exception was thrown ---

at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x00372] in <81c1d29afadd437db7a7f9c0ac3aa46f>:0
at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x002d0] in <81c1d29afadd437db7a7f9c0ac3aa46f>:0
at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x0017e] in <8c3c451ac2a8478d9b689eaba35be510>:0
at NccitDriver.Servizi.JoClass.ChiamataApi (System.String stringa, System.String v) [0x00122] in <2d9806abe2d34a8e8845a2e1101b559f>:0
--- End of managed Java.IO.IOException stack trace ---
java.io.IOException: Canceled
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:156)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)

I've tried to handle exceptions but can't find a solution.

try  
            {                 
                HttpClient client = new HttpClient  
                {  
                    MaxResponseContentBufferSize = 512000,  
                    Timeout = TimeSpan.FromMilliseconds(3000)  
                };  
                HttpResponseMessage response = await client.GetAsync(stringa);  
  
                if (response.IsSuccessStatusCode)  
                {  
                    string content = await response.Content.ReadAsStringAsync();  
                    if (content.Contains("id"))  
                    {  
                        risultato = System.Text.Json.JsonSerializer.Deserialize<Prenotazioni>(content);  
                    }  
                    else  
                    {  
                        risultato = null;  
                    }  
                }  
            }  
              
            catch (TaskCanceledException tcex)  
            {  
                // The request timed out  
            }  
            catch (System.OperationCanceledException Cancellata)  
            {  
                return null;  
            }  
            catch (Exception err)  
            {  
                Log.Debug("ERRORE", err.Message);  
                Log.Debug("ERRORE", err.StackTrace);  
  
            }  
            return risultato;  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,997 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lex Li (Microsoft) 5,582 Reputation points Microsoft Employee
    2022-10-24T19:39:42.327+00:00

    You can report to Microsoft via GitHub to see if this is a known issue of AndroidClientHandler and whether a fix is available,

    https://github.com/xamarin/xamarin-android/issues

    As workaround, you can switch to HttpClientHandler, https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=macos#managed-httpclienthandler

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Murat 101 Reputation points
    2023-08-13T17:48:58.08+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.