Java.Net.SocketException: Software caused connection abort

Vuyiswa Maseko 351 Reputation points
2021-10-01T19:53:20.13+00:00

Good Day All

i have the following code and it randomly gives an error on Android 11

Java.Net.SocketException: Software caused connection abort

, not all the time , App Centre Reported it twice

public async Task<List<USERS>> Get_All_Customers( )
          {

              List<USERS> results = null;
              try
              {
                  USERS m_model = new USERS
                  {
                      USER_ID = 1,
                      CURRENT_DATETIME = '2021/07/01'
                  };
                  var httpClient = new HttpClient();

                  httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                  var Json = JsonConvert.SerializeObject(m_model, new JsonSerializerSettings
                  {
                      ContractResolver = new DefaultContractResolver
                      {
                          IgnoreSerializableAttribute = false
                      }
                  });


                  HttpContent httpContent = new StringContent(Json, Encoding.UTF8, "application/json");
                  httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");

                  var response = await Task.Run(() => httpClient.PostAsync(string.Format("https://www.myserver.com/api/MainCustomers_OnlineDates/Get_All_Customers"), httpContent));

                  if (response.StatusCode == System.Net.HttpStatusCode.OK)
                  {
                      var content = await response.Content.ReadAsStringAsync();
                      results = JsonConvert.DeserializeObject<List<USERS>>(content);
                  }
              }
              catch (Exception ex)
              {
                  await Task.Run(() => Crashes.TrackError(ex));
              }

              return results;
          }

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,872 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
318 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
966 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Vuyiswa Maseko 351 Reputation points
    2021-10-04T15:33:37.61+00:00

    Thanks for the reply. This might have been reported via AppCentre i presume there is no issue there , the issue might be related Xamarin , Android and .net api


  2. Vuyiswa Maseko 351 Reputation points
    2021-10-05T20:35:31.983+00:00

    Thank you , i have made changes to the code .

    1. Created a Static variable for the httpClient that can be used by other functions like this
      static readonly HttpClient httpClient = new HttpClient();
      
    2. and have changed the code by removing the recreation of the variable and also removed Task.Run and my function looks like this
      public async Task<List<USERS>> Get_All_Customers( )
              {
      
                  List<USERS> results = null;
                  try
                  {
                      USERS m_model = new USERS
                      {
                          USER_ID = 1,
                          CURRENT_DATETIME = '2021/07/01'
                      };
                      var httpClient = new HttpClient();
      
                      httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      
      
                      var Json = JsonConvert.SerializeObject(m_model, new JsonSerializerSettings
                      {
                          ContractResolver = new DefaultContractResolver
                          {
                              IgnoreSerializableAttribute = false
                          }
                      });
      
      
                      HttpContent httpContent = new StringContent(Json, Encoding.UTF8, "application/json");
                      httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
      
      var response =  await httpClient.PostAsync(string.Format("https://www.myserver.com/api/MainCustomers_OnlineDates/Get_All_Customers"), httpContent);
      
                      if (response.StatusCode == System.Net.HttpStatusCode.OK)
                      {
                          var content = await response.Content.ReadAsStringAsync();
                          results = JsonConvert.DeserializeObject<List<USERS>>(content);
                      }
                  }
                  catch (Exception ex)
                  {
                      await Task.Run(() => Crashes.TrackError(ex));
                  }
      
                  return results;
              }
      

    you can ignore the date part , just for an example. i will wait and see there is no reports of the same issues

    0 comments No comments