I am using httpclient in my application that was initally design for andriod 7 but in andriod 9 it says here
I am using xamrain forms and this is the code I am using to connect
HttpClient _client;
public RestServices()
{
_client = new HttpClient();
}
public async Task<List<BomLookupModel>> GetBomLookups()
{
List<BomLookupModel> _result = new List<BomLookupModel>();
try
{
var uri = new Uri(string.Format(Constants.GetBomLookupInfo, string.Empty));
var response = await _client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
_result = JsonConvert.DeserializeObject<List<BomLookupModel>>(content);
}
}
catch(Exception ex)
{
Log.Warn(ex.ToString());
}
return _result.ToList();
}
What must I change to get my code to work with a json fetch in android 9 on Xamrian forms? Has anyone else come across this work in Android 7 but according to that article it may not work in Andriod 9.
It just crashes out with a java.io error. The error I get is no firewall is active and my phone is in developer mode any ideas?
192.168.0.13 not permitted at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod
{Java.IO.IOException: Cleartext HTTP traffic to 192.168.0.13 not permitted at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <9ed4c1a7b8844cdcb5330cc881c1cd6a>:0 at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00014] in <9ed4c1a7b8844cdcb5330cc881c1cd6a>:0 at Java.Net.HttpURLConnectionInvoker.Connect () [0x0000a] in :0 at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass44_0.b__0 () [0x0005a] in :0 at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in <3a61d48d66dd458fa0b64990b6c8f8cc>:0 at System.Threading.Tasks.Task.Execute () [0x00000] in <3a61d48d66dd458fa0b64990b6c8f8cc>:0 --- End of stack trace from previous location where exception was thrown ---
at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x0012e] in :0 at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x002be] in :0 at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x000ca] in <162fad71ed6e4a3cb7598d196d170e1b>:0 at FuelStockApp.RESTApi.FuelRestServices.GetBomLookups () [0x00065] in D:\Git\FuelStockAppLive\FuelStockApp\FuelStockApp\FuelRestServices.cs:72 --- End of managed Java.IO.IOException stack trace --- java.io.IOException: Cleartext HTTP traffic to 192.168.0.13 not permitted at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127) }
Edit 2
I have tried the following with Xamrian forms andriod but its not allowing me to still access the http://192 address
<?xml version="1.0" encoding="utf-8"?> <manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.companyname.StockApp" android:installLocation="auto"
android:versionCode="2" android:versionName="1.0"> <uses-sdk
android:minSdkVersion="24" android:targetSdkVersion="28" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission
android:name="android.permission.WRITE_USER_DICTIONARY" />
<application android:label="StockApp.Android"></application>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true">
</application> </manifest>