C
C#7mo ago
المجدد

A problem while making a request for API in maui .net

i try to do request to my api But the application stops I do not get any specific error, but the application stops at the request point and does not receive any response Why ? Does it have anything to do with await async
10 Replies
المجدد
المجددOP7mo ago
If i try to run it without debug it will become like this with console applcation working but with maui no idk why
leowest
leowest7mo ago
so a few things I want to point out first: 1) dont use newtonsoft, its slow, old and only used these days for edge cases, .net already comes with a json serializer/deserializer out of the box System.Text.Json 2) dont use RestSharp its not good, just use httpclient 3) have you configured your maui to accept cleartext? 4) Have you added to your csproj to <EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk> because its complaining of missing files in your output $mauiapihttp
MODiX
MODiX7mo ago
How to access WEB API without dev tunnels in MAUI? To enable UsesCleartextTraffic you open the file Platforms\Android\MainApplication.cs and add the following lines replacing the current [Application]
#if DEBUG
[Application(UsesCleartextTraffic = true)]
#else
[Application]
#endif
#if DEBUG
[Application(UsesCleartextTraffic = true)]
#else
[Application]
#endif
In your asp.net API you would have to do no changes if you're running a emulator in the same machine, if you are using usb, you would have to add to your appsettings.Development.json the following change:
"urls": "http://0.0.0.0:5213"
"urls": "http://0.0.0.0:5213"
Change 5213 to the actual port your API uses.
MODiX
MODiX7mo ago
TeBeCo
* manually serializing string : bad * manually deserializing string then object : bad * leaking UTF8 in Service: bad * leaking mimetype in Service: bad * not using the httpclient from the service: bad * create a single httpclient per post request : bad * create a class named PostRequest to only do a single post, bad * doing the check of internet inside the post request: bad (way too late) * using .Result while you're in an async methid, bad * manipulating StringContent manually bad
Quoted by
<@1102729783969861782> from #mobile (click here)
React with ❌ to remove this embed.
Auger
Auger7mo ago
I'd recommend leowest's suggestions. The cleartext one anyway, as that can cause requests to fail. My assumption of the issue here is that your android emulator is probably not serving HTTP content locally on port 5117 like your PC is. If you inspect the request object, I'd be interested to see why it failed. If you get a HTTP status code, then you know at least it is hitting the HTTP server
المجدد
المجددOP7mo ago
@TeBeCo @leowest @Auger Thank you guys you have already fixed the problem
MODiX
MODiX7mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Trace
Trace7mo ago
agreed
Want results from more Discord servers?
Add your server