0


Ive been playing with .net Maui and had some success doing Bluetooth LE but for reasons I've gone ahead and changed my implementation to use wifi and a restful API. I am building for both android and IoS. I am testing with a real device, running android 12.

I am trying to make a basic http(s) (tried both http and https) request to a local server (an esp32). My url is:

const string url = "https://192.168.4.1/api";

My manifest file:

<?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>

My initialisation function, which gets called when my page is loaded - MainPage()

private void init_network()
{
    client = new HttpClient();

    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
}

and finally, my send function which gets called by a button pressed event

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;
}

I also have a runtime permissions section which asks for location_fine_access, which I've given access for.
When running through the request function, I get an exception thrown with the message:

[DOTNET] Operation is not supported on this platform.

I thought this was a permissions issue, and so android isn't letting me make the request but now I'm not so sure. Anyone else had and resolved this issue?

Tokens94
  • 113
  • 1
  • 8
  • Can you see which line of code exactly triggers this error? – Gerald Versluis Jun 22 '22 at 10:29
  • The url in your code is wrong. For android, the ip address always be 10.0.0.2 and also need a port number. So you can try to change the value of the url and try again. – Liyun Zhang - MSFT Jun 23 '22 at 02:23
  • Ive added a comment after the line which throws the exception above. Ive tried changing the ip address and setting a port, but i totally disagree. The url is my TARGET web address, which is a static ip address I've set on my network. I can access the api from a browser, I cant access the api from my app because of the above error. – Tokens94 Jun 23 '22 at 09:58
  • You can check the [official document](https://developer.android.com/studio/run/emulator-networking) about the IP address for the android simulator. – Liyun Zhang - MSFT Jun 24 '22 at 06:53
  • Sorry, I didnt specify that I am using a real device, not a simulator. I am using an android phone running android 12 (build tools 31), which I can access the webpage from a browser (chrome and firefox), so its not an address issue. – Tokens94 Jun 28 '22 at 09:37
  • There is a [similar case](https://stackoverflow.com/questions/72313635/net-maui-android-cant-talk-to-api-localhost) which did the same thing and meet the same problem. – Liyun Zhang - MSFT Jun 29 '22 at 07:45

1 Answers1

0

Looks as though .net maui doesnt hook into the android http client properly and is an issue on github. https://github.com/dotnet/maui/issues/1260 the suggested work around, which I have tested and works for me, is to instantiate your HttpClient with the native android handler like this as a workaround:

new HttpClient(new Xamarin.Android.Net.AndroidMessageHandler()).
Tokens94
  • 113
  • 1
  • 8