简体   繁体   English

Xamarin - 无法通过 Android(和 iOS)访问本地 api

[英]Xamarin - unable to access local api via Android (and iOS)

I'm using macos Rider to develop a Xamarin app for Android and iOs.我正在使用 macos Rider 为 Android 和 iOs 开发 Xamarin 应用程序。 I have a local api that I'm trying to use via the app, but it appears that the app cannot reach the api.我有一个本地 api,我试图通过该应用程序使用它,但该应用程序似乎无法访问该 api。 This happens on Android as well as on iOS.这发生在 Android 和 iOS 上。

Focussing on Android, I have tried changing the api to http://10.0.2.2:5000 .专注于 Android,我尝试将 api 更改为http://10.0.2.2:5000 The thing is, I can access the api endpoints when using the emulators browser, but trying to do so via the app, it gets stuck on loading.问题是,我可以在使用模拟器浏览器时访问 api 端点,但是尝试通过应用程序访问时,它在加载时卡住了。

When using the iPhone emulator, I get the error that I should use SSL in order to access the api via the emulator.使用 iPhone 模拟器时,我收到错误消息,我应该使用 SSL 才能通过模拟器访问 api。

I've tried to debug on Android in order to find out whether the api is actually the issue here (I find it weird that I can access it via the emulators browser), but I'm unable to figure it out because there is no exception thrown.我试图在 Android 上进行调试,以找出 api 是否真的是这里的问题(我发现我可以通过模拟器浏览器访问它很奇怪),但我无法弄清楚,因为没有抛出异常。 When reaching the code below, it appears as if it simply 'stops'.当到达下面的代码时,它看起来好像只是“停止”了。 在此处输入图片说明

To sum it up, some facts:总结一下,一些事实:

  1. The app breaks down on iOS and Android该应用程序在 iOS 和 Android 上出现故障
  2. The api is running, and I've tried http://10.0.2.2:5000 for Android, http://localhost:5000 for iPhone. api 正在运行,我试过http://10.0.2.2:5000 for Android, http://localhost:5000 for iPhone。 I can reach the app via both emulators browsers and via Postman.我可以通过模拟器浏览器和 Postman 访问该应用程序。 This gives me results as well, so nothing appears to be wrong with the database or the api for that matter.这也给了我结果,因此数据库或 api 似乎没有任何问题。
  3. I'm using a Mac (Jetbrains Rider)我使用的是 Mac (Jetbrains Rider)
  4. It does work when I use the same api that is running on a server当我使用在服务器上运行的相同 api 时它确实有效

If you want to access HTTP connections to any site , You need to add the following code in specific platform如果要访问任何站点的HTTP连接,则需要在特定平台添加以下代码

in iOS在 iOS 中

Add the following lines to info.plist将以下行添加到info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

in Android在安卓中

Add the line android:usesCleartextTraffic="true" to AndroidManifest like following将行android:usesCleartextTraffic="true"AndroidManifest ,如下所示

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"   android:versionCode="1" android:versionName="1.0" package="com.companyname.app32">
    <uses-sdk android:minSdkVersion="21"  android:targetSdkVersion="28"  />
    <application android:label="App32.Android" android:theme="@style/MainTheme" android:usesCleartextTraffic="true"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM