Xamarin Android PCL Web请求挂起

Xamarin Android PCL Web请求挂起,android,xamarin.android,Android,Xamarin.android,我正在尝试制作一个PCL,它可以为Xamarin Android项目发出web请求 我的androidmanifest.xml如下所示 <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="JJJ.Mobile.Android" android:installLocation="auto"&

我正在尝试制作一个PCL,它可以为Xamarin Android项目发出web请求

我的androidmanifest.xml如下所示

<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主活动按钮click中的代码直接调用这个PCL创建方法

我在执行SendAsync和返回null的行上有断点,SendAsync断点会被命中,当你跨过它VS挂起一秒钟时,模拟器就会显示,应用程序挂起-Android会在收到关于不响应等待或不响应的消息后立即显示-异常断点永远不会被命中。控制台中未显示任何异常

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;
            }
        }
公共异步任务创建() { 尝试 { var uri=新的uri(“http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/198440/allinfo"); var httpClient=新的httpClient(); HttpRequestMessage请求=新的HttpRequestMessage(HttpMethod.Get,uri); var response=wait httpClient.sendaync(请求); 返回响应; } 捕获(例外情况除外) { 返回null; } } 单击按钮后的控制台输出,不确定是否有用

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).
线程已启动:#6
03-30 19:31:17.669 D/Mono(2556):Assembly Ref addref System.Net.Http[0x9d63d4e0]->System.Core[0x9e0abbe0]:10
线程已启动:#7
03-30 19:31:18.392 D/Mono(2556):[0x9d7bf930]爬山,更改最大螺纹数3
线程已启动:#8
03-30 19:31:18.490 D/Mono(2556):[0x9adbf930]工作进程正在启动
03-30 19:31:18.538 D/Mono(2556):正在卸载image System.dll[0x9ae04f00]。
03-30 19:31:18.539 D/Mono(2556):图像添加参考系统[0xaed95740]->System.dll[0xaadd4900]:5
03-30 19:31:18.539 D/Mono(2556):配置正在尝试解析:“System.dll.Config”。
03-30 19:31:18.539 D/Mono(2556):试图解析的配置:“/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/Mono-x86/etc/Mono/assemblems/System/System.Config”。
03-30 19:31:18.540 D/Mono(2556):正在卸载image System.dll[0x9ae04f00]。
03-30 19:31:18.540 D/Mono(2556):图像添加参考系统[0xaed95920]->System.dll[0xaadd4900]:6
03-30 19:31:18.541 D/Mono(2556):配置正在尝试解析:“System.dll.Config”。
03-30 19:31:18.541 D/Mono(2556):试图解析的配置:“/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/Mono-x86/etc/Mono/assemblems/System/System.Config”。
03-30 19:31:18.543 D/Mono(2556):正在卸载image System.dll[0x9ae04f00]。
03-30 19:31:18.543 D/Mono(2556):图像添加参考系统[0xaed95920]->System.dll[0xaadd4900]:7
03-30 19:31:18.543 D/Mono(2556):配置正在尝试解析:“System.dll.Config”。
03-30 19:31:18.543 D/Mono(2556):试图解析的配置:“/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/Mono-x86/etc/Mono/assemblems/System/System.Config”。
03-30 19:31:18.545 D/Mono(2556):正在卸载image System.dll[0x9ae04f00]。
03-30 19:31:18.545 D/Mono(2556):图像添加参考系统[0xaed95920]->System.dll[0xaadd4900]:8
03-30 19:31:18.545 D/Mono(2556):配置正在尝试解析:“System.dll.Config”。
03-30 19:31:18.545 D/Mono(2556):试图解析的配置:“/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/Mono-x86/etc/Mono/assemblems/System/System.Config”。
03-30 19:31:18.573 D/Mono(2556):DllImport在以下位置搜索:“_Internal”(“(null)”)。
03-30 19:31:18.573 D/Mono(2556):搜索“java_interop_jnienv_is_assignable_from”。
03-30 19:31:18.573 D/Mono(2556):探测“java_interop_jnienv_是可分配的”。
03-30 19:31:18.573 D/Mono(2556):被发现为“java_interop_jnienv_is_assignable_from”。
03-30 19:31:18.574 D/Mono(2556):DllImport在以下位置搜索:“_Internal”(“(null)”)。
03-30 19:31:18.574 D/Mono(2556):搜索“java_interop_jnienv_get_byte_array_region”。
03-30 19:31:18.574 D/Mono(2556):探测“java_interop_jnienv_get_byte_array_region”。
03-30 19:31:18.574 D/Mono(2556):被发现为“java_interop_jnienv_get_byte_array_region”。
03-30 19:31:18.575 D/Mono(2556):DllImport在以下位置搜索:“_Internal”(“(null)”)。
03-30 19:31:18.575 D/Mono(2556):搜索“java_interop_jnienv_set_byte_array_region”。
03-30 19:31:18.575 D/Mono(2556):探测“java_interop_jnienv_set_byte_array_region”。
03-30 19:31:18.575 D/Mono(2556):被发现为“java_interop_jnienv_set_byte_array_region”。
03-30 19:31:18.632 W/Mono(2556):加载组装系统的请求。Core v4.0.0.0已重新映射到v2.0.5.0
03-30 19:31:18.632 D/Mono(2556):卸载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):配置正在尝试解析:“System.Core.dll.Config”。
03-30 19:31:18.632 D/Mono(2556):试图解析的配置:“/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/Mono-x86/etc/Mono/assemblems/System.Core/System.Core.Config”。
03-30 19:31:18.640 W/Mono(2556):加载组装系统的请求。Core v4.0.0.0已重新映射到v2.0.5.0
03-30 19:31:18.641 D/Mono(2556):卸载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):配置正在尝试解析:“System.Core.dll.Config”。
03-30 19:31:18.641 D/Mono(2556):试图解析的配置:“/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/Mono-x86/etc/Mono/assemblems/System.Core/System.Core.Config”。
03-30 19:31:18.895 D/Mono(2556):[0x9d7bf930]爬山,更改最大螺纹数2
03-30 19:31:24.114 D/Mono(2556):[0x9adbf930]工人整理
螺纹加工:#8
线程“未知”(0x8)ha
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;
    }
}
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();
        }
    }
}
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;
        }
    }
}