gpt4 book ai didi

android - Xamarin Android PCL Web 请求挂起

转载 作者:行者123 更新时间:2023-11-30 00:37:48 25 4
gpt4 key购买 nike

我正在尝试制作一个可以为 Xamarin Android 项目发出 Web 请求的 PCL。

我的 AndoridManifest.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 主 Activity 按钮点击中的代码只是直接调用此 PCL Create 方法。

我在它执行 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;
}
}

点击按钮后的控制台输出,不知道有没有帮助

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,我已经使用内置浏览器检查了互联网,它可以正常工作。我真的很困惑,任何帮助将不胜感激!

最佳答案

我假设您的内存不足,这迫使您的任务终止。我使用了下面的代码,一切都按预期工作。我将项目设置为 Xamarin.Android 项目,并为请求部分使用 Xamarin.PCL。我还创建了一个模拟模型,以更简洁的方式获得响应。我真的不知道你在用 XML 做什么,所以如果它不相关,你可以省略那部分。

项目设置

enter image description here

PCL 中安装的 Nuget 包

现代HttpClient
牛顿软件.Json

主要 Activity
下面是 Android 主 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 中的结果
enter image description here

关于android - Xamarin Android PCL Web 请求挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43125802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com