我一直在玩.net Maui,并且在蓝牙LE上取得了一些成功,但是出于一些原因,我已经开始使用wifi和restful来实现我的实现。我正在为安卓和IoS开发。我正在用一个真正的设备进行测试,运行android 12。
我正在尝试向本地服务器( esp32)发出基本的http (尝试了http和https)请求。我的网址是:
const string url = "https://192.168.4.1/api";我的舱单档案:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="28" android:targetSdkVersion="31" />
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>我的初始化函数,它在加载页面时被调用- MainPage()
private void init_network()
{
client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
}最后,我的发送函数被按下按钮事件调用。
private bool SendCommand(string command)
{
try
{
var msg = new HttpRequestMessage(HttpMethod.Post, url);
msg.Content = JsonContent.Create(command);
HttpResponseMessage response = client.Send(msg); // line where the code throws exception
if (response.IsSuccessStatusCode)
{
return true;
}
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
return false;
}我还有一个运行时权限部分,它请求location_fine_access,我已经为它提供了访问权限。
在运行请求函数时,我会看到一个异常,其中包含以下消息:
[DOTNET] Operation is not supported on this platform.我认为这是一个权限问题,所以android不允许我提出请求,但现在我不太确定。还有其他人解决了这个问题吗?
发布于 2022-06-29 09:54:03
看起来.net maui并没有正确地连接到android客户端,这是github上的一个问题。https://github.com/dotnet/maui/issues/1260建议的工作,我已经测试过,并为我工作,是实例化您的HttpClient与本机HttpClient处理程序,作为一个解决办法如下:
new HttpClient(new Xamarin.Android.Net.AndroidMessageHandler()).https://stackoverflow.com/questions/72713640
复制相似问题