问题
I am trying to make a PCL that can make web requests for a Xamarin Android project.
My AndoridManifest.xml looks like as follows
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="JJJ.Mobile.Android" android:installLocation="auto">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application android:allowBackup="true" android:label="@string/app_name" android:theme="@style/MyTheme"></application>
</manifest>
The code in the Android main activity button click just calls this PCL Create method directly.
I have breakpoints on the lines where it does SendAsync and where returns null, the SendAsync breakpoint gets hit and when you step over it VS hangs for a second then the Emulator is displayed and the app is hanging - Android displays soon after a message about not responding to wait or not - the exception breakpoint never gets hit. No exception is shown in the console
public async Task<HttpResponseMessage> Create()
{
try
{
var uri = new Uri("http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/198440/allinfo");
var httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
var response = await httpClient.SendAsync(request);
return response;
}
catch (Exception ex)
{
return null;
}
}
The console output after button clicked, not sure if it's helpful or not
Thread started: #6
03-30 19:31:17.669 D/Mono ( 2556): Assembly Ref addref System.Net.Http[0x9d63d4e0] -> System.Core[0x9e0abbe0]: 10
Thread started: <Thread Pool> #7
03-30 19:31:18.392 D/Mono ( 2556): [0x9d7bf930] hill climbing, change max number of threads 3
Thread started: <Thread Pool> #8
03-30 19:31:18.490 D/Mono ( 2556): [0x9adbf930] worker starting
03-30 19:31:18.538 D/Mono ( 2556): Unloading image System.dll [0x9ae04f00].
03-30 19:31:18.539 D/Mono ( 2556): Image addref System[0xaed95740] -> System.dll[0xaadd4900]: 5
03-30 19:31:18.539 D/Mono ( 2556): Config attempting to parse: 'System.dll.config'.
03-30 19:31:18.539 D/Mono ( 2556): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System/System.config'.
03-30 19:31:18.540 D/Mono ( 2556): Unloading image System.dll [0x9ae04f00].
03-30 19:31:18.540 D/Mono ( 2556): Image addref System[0xaed95920] -> System.dll[0xaadd4900]: 6
03-30 19:31:18.541 D/Mono ( 2556): Config attempting to parse: 'System.dll.config'.
03-30 19:31:18.541 D/Mono ( 2556): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System/System.config'.
03-30 19:31:18.543 D/Mono ( 2556): Unloading image System.dll [0x9ae04f00].
03-30 19:31:18.543 D/Mono ( 2556): Image addref System[0xaed95920] -> System.dll[0xaadd4900]: 7
03-30 19:31:18.543 D/Mono ( 2556): Config attempting to parse: 'System.dll.config'.
03-30 19:31:18.543 D/Mono ( 2556): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System/System.config'.
03-30 19:31:18.545 D/Mono ( 2556): Unloading image System.dll [0x9ae04f00].
03-30 19:31:18.545 D/Mono ( 2556): Image addref System[0xaed95920] -> System.dll[0xaadd4900]: 8
03-30 19:31:18.545 D/Mono ( 2556): Config attempting to parse: 'System.dll.config'.
03-30 19:31:18.545 D/Mono ( 2556): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System/System.config'.
03-30 19:31:18.573 D/Mono ( 2556): DllImport searching in: '__Internal' ('(null)').
03-30 19:31:18.573 D/Mono ( 2556): Searching for 'java_interop_jnienv_is_assignable_from'.
03-30 19:31:18.573 D/Mono ( 2556): Probing 'java_interop_jnienv_is_assignable_from'.
03-30 19:31:18.573 D/Mono ( 2556): Found as 'java_interop_jnienv_is_assignable_from'.
03-30 19:31:18.574 D/Mono ( 2556): DllImport searching in: '__Internal' ('(null)').
03-30 19:31:18.574 D/Mono ( 2556): Searching for 'java_interop_jnienv_get_byte_array_region'.
03-30 19:31:18.574 D/Mono ( 2556): Probing 'java_interop_jnienv_get_byte_array_region'.
03-30 19:31:18.574 D/Mono ( 2556): Found as 'java_interop_jnienv_get_byte_array_region'.
03-30 19:31:18.575 D/Mono ( 2556): DllImport searching in: '__Internal' ('(null)').
03-30 19:31:18.575 D/Mono ( 2556): Searching for 'java_interop_jnienv_set_byte_array_region'.
03-30 19:31:18.575 D/Mono ( 2556): Probing 'java_interop_jnienv_set_byte_array_region'.
03-30 19:31:18.575 D/Mono ( 2556): Found as 'java_interop_jnienv_set_byte_array_region'.
03-30 19:31:18.632 W/Mono ( 2556): The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0
03-30 19:31:18.632 D/Mono ( 2556): Unloading image System.Core.dll [0x9ae06d00].
03-30 19:31:18.632 D/Mono ( 2556): Image addref System.Core[0xaed96460] -> System.Core.dll[0xaadd6700]: 5
03-30 19:31:18.632 D/Mono ( 2556): Config attempting to parse: 'System.Core.dll.config'.
03-30 19:31:18.632 D/Mono ( 2556): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System.Core/System.Core.config'.
03-30 19:31:18.640 W/Mono ( 2556): The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0
03-30 19:31:18.641 D/Mono ( 2556): Unloading image System.Core.dll [0x9ae05e00].
03-30 19:31:18.641 D/Mono ( 2556): Image addref System.Core[0xaed96460] -> System.Core.dll[0xaadd6700]: 6
03-30 19:31:18.641 D/Mono ( 2556): Config attempting to parse: 'System.Core.dll.config'.
03-30 19:31:18.641 D/Mono ( 2556): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System.Core/System.Core.config'.
03-30 19:31:18.895 D/Mono ( 2556): [0x9d7bf930] hill climbing, change max number of threads 2
03-30 19:31:24.114 D/Mono ( 2556): [0x9adbf930] worker finishing
Thread finished: <Thread Pool> #8
The thread 'Unknown' (0x8) has exited with code 0 (0x0).
The emulator is running Android 6, I've checked the internet by using the built in browser and it works. I am really confused about this, any help would be greatly appreciated!
回答1:
I assume that you're running out of memory which is forcing your task to terminate. I used the code below and all is working as expected. I setup the project as a Xamarin.Android project with a Xamarin.PCL for the request portion. I also created a mock model for getting the response in a cleaner fashion. I don't really know what you're doing with the XML so you could omit that part if it is not relevant.
Project Setup
Nuget Packages Installed in the PCL
ModernHttpClient
Newtonsoft.Json
Main Activity
Below is the Android Main Activity
using Android.App;
using Android.OS;
using XamPCL;
namespace App1
{
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var n = new XamPCLa();
//you want to await this function.
Model response = await n.Test();
}
}
}
Xamarin.Portable (XamPCL)
using ModernHttpClient;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace XamPCL
{
public class Model
{
/// <summary>
/// this will resolve your xml into the model
/// </summary>
[JsonProperty(PropertyName = "rxtermsProperties")]
public DrugSpec Container {get;set;}
}
public class DrugSpec
{
/// <summary>
///
/// </summary>
[JsonProperty(PropertyName = "displayName")]
public string DisplayName { get; set; }
}
public class XamPCLa
{
public XamPCLa()
{
}
public async Task<Model> Test()
{
var httpClient = GetHttpClient();
try
{
var response = await httpClient.GetAsync(@"https://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/198440/allinfo").ConfigureAwait(false);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
var content = response.Content;
string jsonString = await content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<Model>(jsonString);
}
}
catch (Exception e)
{
}
return new Model();
}
private HttpClient GetHttpClient()
{
var httpClient = new HttpClient(new NativeMessageHandler());
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return httpClient;
}
}
}
Result in MainActivity
回答2:
Not sure what is causing that. However, I am able to do GET request to my web service. I am using WebRequest instead. I will provide the code below and you could try that.
public async Task<WebResponse> Create()
{
var uri = "http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/198440/allinfo";
WebRequest request = WebRequest.CreateHttp(uri);
string responseValue = null;
try
{
using (var response = await request.GetResponseAsync())
{
using (var stream = response.GetResponseStream())
{
if (stream != null)
{
using (var reader = new StreamReader(stream))
{
responseValue = await reader.ReadToEndAsync();
}
}
}
}
return responseValue;
}
catch (Exception ex)
{
return null;
}
}
Hope this could help.
来源:https://stackoverflow.com/questions/43125802/xamarin-android-pcl-web-request-hangs