Why do I keep getting below error randomly? I have noticed when I navigate between pages fast then I get this error but when I slow down my navigation it does not show.

How to solve this?

Java.IO.IOException: unexpected end of stream on com.android.okhttp.Address@5a70b0d9 ---> Java.IO.EOFException: \n not found: size=0 content=...
at java.io.EOFException: \n not found: size=0 content=...
at at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:212)
at at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:395)
at at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:146)
at at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:900)
at at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:772)
at at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:475)
at at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
at at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
at at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
at at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
--- End of inner exception stack trace ---
at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <1959115d56f8444789986cf39185638c>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <1959115d56f8444789986cf39185638c>:0
at Java.Net.HttpURLConnection.get_ResponseCode () [0x00000] in /Users/builder/azdo/_work/2/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-30/mcw/Java.Net.HttpURLConnection.cs:602
at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass46_0.<DoProcessRequest>b__2 () [0x00000] in /Users/builder/azdo/_work/2/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:439
at System.Threading.Tasks.Task 1[TResult].InnerInvoke () [0x0000f] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:534 at System.Threading.Tasks.Task.Execute () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2319 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) [0x00328] in /Users/builder/azdo/_work/2/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:439 at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00286] in /Users/builder/azdo/_work/2/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:287 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 /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:506
at Zeera.NewPost.ButtonNewPostNext_Clicked (System.Object sender, System.EventArgs e) [0x01125] in /Users/jassim/Projects/Zeera/Zeera/NewPost.xaml.cs:464
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in /Users/builder/azdo/_work/2/s/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:36
at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in /Users/builder/azdo/_work/2/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36
at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in /Users/builder/azdo/_work/2/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-30/mcw/Java.Lang.IRunnable.cs:90
at at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.52(intptr,intptr)
at java.io.IOException: unexpected end of stream on com.android.okhttp.Address@5a70b0d9
at at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:410)
at at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:146)
at at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:900)
at at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:772)
at at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:475)
at at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
at at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
at at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
at at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
at Caused by: java.io.EOFException: \n not found: size=0 content=...
at at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:212)
at at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:395)
at ... 8 more

Welcome to our Microsoft Q&A platform!

Java.IO.EOFException: \n not found: size=0 content=...

If you get EOF exception, When the interval between two requests exceeds the keep-alive timeout configured on the server side, the server side will actively close the current connection. The Okhttp connection pool ConnectionPool default timeout time is 5min , which means that the connection in the connection pool will be reused first, but the server has already taken the initiative close, causing the input stream to be interrupted, and then throwing an EOF exception. Among them, keep-alive : the maximum interval between two requests. If this interval is exceeded, the server will actively close the connection, mainly to avoid re-establishing the connection and saving server resources.

When creating an OkHttpClient instance, you only need to set ** RetryOnConnectionFailure(true);** to true. When an EOF exception is thrown, let it continue to execute the realChain.proceed method.

Or store information to the location storage, If navigate pages , please compare two connection times, if within default timeout time, use the location information.

====================
Update ==========================

I find a new way to fix it, please try to achieve the Httpclient request with try catch method. if we get Java.IO.IOException: unexpected end of stream.. exception, we load the data from the local storage, I use sharepreference to make a test. If the HttpStatusCode.OK , we can store the data to the local storage when get the Java.IO.IOException: unexpected end of stream.. , please note, I cannot get this execption to make a test, so the exception's string is wrong, you can debug it then get the correctly execption.

   var client = new HttpClient();  
               client.Timeout = TimeSpan.FromSeconds(App.HttpClient_TimeOut);  
               client.BaseAddress = new Uri("https://www.domain.com/get.php");  
               string result;  
               var content = new FormUrlEncodedContent(new[]  
                    new KeyValuePair<string, string>("id", id),  
                    var response =  await client.PostAsync("https://www.domain.com/get.php", content);  
                   if (response.StatusCode == HttpStatusCode.OK)  
                       //store the information to the local file or share-preference  
                       result = await response.Content.ReadAsStringAsync();  
                       Preferences.Set("my_key", result);  
               catch (Exception exc) {  
                   if (exc.Message == "Java.IO.IOException: unexpected end of stream..")  
                       result= Preferences.Get("my_key", "default_value");  

Best Regards,

Leon Lu

If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

How can I use RetryOnConnectionFailure with Xamarin?

my connection is like this:

var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(App.HttpClient_TimeOut);
client.BaseAddress = new Uri("https://www.domain.com/get.php");
var content = new FormUrlEncodedContent(new[]
    new KeyValuePair<string, string>("id", id),
var response = await client.PostAsync("https://www.domain.com/get.php", content);
var result = await response.Content.ReadAsStringAsync();
												

Yes, HttpClient do not have this API to fix this error, But use Try/catch for post request. this is a normal way, because network is unstabled in different environment.

Or You can try to use depedenceService to use OkHttpclient and set RetryOnConnectionFailure(true); to true for android.