Xamarin Android PCL Web 请求挂起 [英] Xamarin Android PCL Web Request Hangs
问题描述
我正在尝试制作一个可以为 Xamarin Android 项目发出 Web 请求的 PCL.
I am trying to make a PCL that can make web requests for a Xamarin Android project.
我的 AndoridManifest.xml 如下所示
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>
Android 主活动按钮点击中的代码只是直接调用了这个 PCL Create 方法.
The code in the Android main activity button click just calls this PCL Create method directly.
我在执行 SendAsync 和返回 null 的行上有断点,SendAsync 断点被击中,当你跨过它时 VS 挂起一秒钟,然后显示模拟器并且应用程序挂起 - Android 在关于不响应等待或不响应的消息 - 异常断点永远不会被命中.控制台无异常显示
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).
模拟器运行的是 Android 6,我已经使用内置浏览器检查了互联网,并且可以正常工作.我真的对此感到困惑,任何帮助将不胜感激!
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!
推荐答案
我假设您的内存不足,这迫使您的任务终止.我使用了下面的代码,一切都按预期工作.我将项目设置为 Xamarin.Android 项目,请求部分使用 Xamarin.PCL.我还创建了一个模拟模型,以更简洁的方式获得响应.我真的不知道你在用 XML 做什么,所以如果它不相关,你可以省略那部分.
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.
项目设置
安装在 PCL 中的 Nuget 包
现代Http客户端
Newtonsoft.Json
Nuget Packages Installed in the PCL
ModernHttpClient
Newtonsoft.Json
主要活动
下面是 Android 的主要活动
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;
}
}
}
MainActivity 中的结果
这篇关于Xamarin Android PCL Web 请求挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!