【问题标题】:Xamarin Android: HttpRequestException: No route to hostXamarin Android:HttpRequestException:没有路由到主机
【发布时间】:2019-11-04 14:03:57
【问题描述】:

我的应用程序以前运行良好,但从几天开始,没有任何变化,我得到了异常

System.Net.Http.HttpRequestException: No route to host

完整的例外如下。

{System.Net.Http.HttpRequestException: 没有到主机的路由 ---> System.Net.Sockets.SocketException: 没有到主机的路由 在 System.Net.Http.ConnectHelper.ConnectAsync(System.String 主机,System.Int32 端口,System.Threading.CancellationToken cancelToken)[0x00110] 在 :0 --- 内部异常堆栈跟踪结束 --- 在 System.Net.Http.ConnectHelper.ConnectAsync(System.String 主机,System.Int32 端口,System.Threading.CancellationToken cancelToken)[0x001ac] 在 :0 在 System.Threading.Tasks.ValueTask1[TResult].get_Result () [0x0001b] in <6de489942345dbea42342d8c6>:0 at System.Net.Http.HttpConnectionPool.CreateConnectionAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00134] in <99e6bf9d99304315ba19eacc7497e256>:0 at System.Threading.Tasks.ValueTask1[TResult].get_Result () [0x0001b] 在 :0 在 System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync (System.Threading.Tasks.ValueTask1[TResult] creationTask) [0x000a2] in <99e6bf9d9342043152342eacc7497e256>:0 at System.Threading.Tasks.ValueTask1[TResult].get_Result () [0x0001b] 在 :0 在 System.Net.Http.HttpConnectionPool.SendWithRetryAsync (System.Net.Http.HttpRequestMessage 请求,System.Boolean doRequestAuth,System.Threading.CancellationToken cancelToken) [0x00089] 在 :0 在 System.Net.Http.RedirectHandler.SendAsync (System.Net.Http.HttpRequestMessage 请求,System.Threading.CancellationToken cancelToken) [0x000ba] 在 :0 在 System.Net.Http.HttpClient.FinishSendAsyncUnbuffered (System.Threading.Tasks.Task1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x000b3] in <99e6bf9d99304315ba19eacc7497e256>:0 at Refit.RequestBuilderImplementation+<>c__DisplayClass11_01[T].b__0 (System.Net.Http.HttpClient 客户端, System.Threading.CancellationToken ct, System.Object[] paramList ) [0x00115] 在 :0 在 MyApp.Droid.Activities.ActivityLogin.GetToken () [0x000c2] in /ActivityLogin.cs:624 }

我的网址类似于https://someservice.com:8080 我还在 android 清单文件中添加了 android:usesCleartextTraffic="true" 。 请在这方面指导我。

【问题讨论】:

  • 你的服务器工作正常吗?
  • 是的,我收到了邮递员的回复。它工作正常。
  • 我的应用程序运行良好,但几天前,它崩溃了。当我通过代码进行调试时,我得到了上述异常。
  • 你想用代码做什么?使用应用程序连接到 url?如果是,您如何连接?
  • 你使用的是localhost还是公共网页?

标签: android xamarin.android


【解决方案1】:

如果它与 SSL 相关,请尝试以下解决方案可能对您有用

xml 目录中创建一个文件,名称为network_security_configx.xml,代码如下
res -> xml -> network_security_configx.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

然后在您的AndroidManifest &lt;application&gt; 中添加以下代码

<application
        android:name=".volley.AppController"
        android:allowBackup="true"
        android:fullBackupContent="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:usesCleartextTraffic="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning"
       >
.....
</application>

【讨论】:

    【解决方案2】:

    这看起来像是 DNS 问题,请尝试使用带有 API 托管服务器的直接地址的基本 URL,而不是使用域/子域来测试是否适合您。

    【讨论】:

      【解决方案3】:

      我已经测试了上述所有场景,但没有任何技巧。我正在办公室测试该应用程序。当我在家中测试同一个应用程序时,它运行良好。问题是由于办公网络的限制。 . .端口也不允许,所以发生了这个问题。现在对我的设备的限制已解除,因此应用程序在办公室也能正常运行。

      【讨论】:

        【解决方案4】:

        是的,在使用 API 时,移动应用程序经常会发生这种情况,因此为了解决这类问题,我使用 Polly 库来实现弹性,使用 Policy WaitAndRetryAsync 与 Xamarin 一起工作非常好:

        await Policy
                .Handle<Exception>()
                .WaitAndRetryAsync
                (
                    retryCount: 3,
                    sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                    onRetry: (ex, time) =>
                    {
                        IsError = true;//bool that show the text with the message
                        cont++;
                        Mensage = $"Error on load data: {ex.Message}, trying again ({cont})..";
                        Crashes.TrackError(ex);//using the library of app center to register the exception
                    }
                )
                .ExecuteAsync(async () =>
                {
                    //your code to call the API;                              
                });
        

        【讨论】:

          猜你喜欢
          • 2013-01-12
          • 1970-01-01
          • 2019-03-08
          • 2017-08-03
          • 2019-03-08
          • 2018-02-07
          • 2011-12-15
          • 1970-01-01
          相关资源
          最近更新 更多