async/await 方法在 Android 中使 Web 服务响应崩溃,但相同的代码在 Window Phone 和 iOS 应用程序中成功运行在 xamarin 本机应用程序中
async/await method crash web services response in Android but same code work successfully in Window Phone and iOS apps in xamarin Native Application
async/await 方法在 Android 中使 Web 服务响应崩溃,但相同的代码在 Window Phone 和 iOS 中的应用程序中成功运行xamarin 本机应用程序。
Xamarin 本机应用程序 (PCL)
我使用可移植 Class 库创建了一个本机应用程序。
我请求 REST API 给出以下响应。
{
code: 200,
status: "ok",
message: "hello"
}
我的便携式 class 库代码在 xamarin.forms 中是
test.xaml.cs
public class jsonResponseClass
{
public string code { get; set; }
public string status { get; set; }
public string message { get; set; }
}
public partial class test : ContentPage
{
public async void getDatas()
{
var cl = new HttpClient();
var result = await cl.GetStringAsync("http://192.168.1.125/apps/jara/web/api/user/test");
jsonResponseClass des = JsonConvert.DeserializeObject<jsonResponseClass>(result);
lbl1.Text = des.code + " " + des.status + " " + des.message;
}
public test()
{
InitializeComponent();
getDatas();
}
}
便携式 class 库中的主文件 (PCL)
App.cs
namespace NativeAppsWithWCF
{
public class App : Application
{
public App()
{
MainPage = new test();
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
我的 Visual studio 解决方案资源管理器
使用上面的代码,我可以在 iOS 和 Window Phone 应用程序中成功显示输出。
但是当我 运行 Android 应用程序崩溃并且不显示输出时。
当我调试 Android 程序时,光标会在这一行之后移除。
var result = await
cl.GetStringAsync("http://192.168.1.125/apps/jara/web/api/user/test");
在这行之后停止程序的执行。
这意味着它不会等待方法完成执行
报错后看画面..
如何防止这个错误?
经过大量的研发终于解决了这个问题
网络连接问题。
我在 Android 模拟器中设置网络连接后解决了这个问题。
按照下面的步骤..
打开 VS Android 模拟器 >> 进入设置 >> 打开 WIFI >>
有 "Wired eth1" 个网络可用。
长按 "Wired eth1" 网络
然后 单击 >> 修改网络 >> 将 "IP Setting" 更改为 "Static" >>
更改网络的 IP 地址。
进行此设置后,我 解决了 我的问题,并从 REST API.
获得了回复
谢谢。
async/await 方法在 Android 中使 Web 服务响应崩溃,但相同的代码在 Window Phone 和 iOS 中的应用程序中成功运行xamarin 本机应用程序。
Xamarin 本机应用程序 (PCL)
我使用可移植 Class 库创建了一个本机应用程序。
我请求 REST API 给出以下响应。
{
code: 200,
status: "ok",
message: "hello"
}
我的便携式 class 库代码在 xamarin.forms 中是
test.xaml.cs
public class jsonResponseClass
{
public string code { get; set; }
public string status { get; set; }
public string message { get; set; }
}
public partial class test : ContentPage
{
public async void getDatas()
{
var cl = new HttpClient();
var result = await cl.GetStringAsync("http://192.168.1.125/apps/jara/web/api/user/test");
jsonResponseClass des = JsonConvert.DeserializeObject<jsonResponseClass>(result);
lbl1.Text = des.code + " " + des.status + " " + des.message;
}
public test()
{
InitializeComponent();
getDatas();
}
}
便携式 class 库中的主文件 (PCL)
App.cs
namespace NativeAppsWithWCF
{
public class App : Application
{
public App()
{
MainPage = new test();
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
我的 Visual studio 解决方案资源管理器
使用上面的代码,我可以在 iOS 和 Window Phone 应用程序中成功显示输出。
但是当我 运行 Android 应用程序崩溃并且不显示输出时。
当我调试 Android 程序时,光标会在这一行之后移除。
var result = await
cl.GetStringAsync("http://192.168.1.125/apps/jara/web/api/user/test");
在这行之后停止程序的执行。
这意味着它不会等待方法完成执行
报错后看画面..
如何防止这个错误?
经过大量的研发终于解决了这个问题
网络连接问题。
我在 Android 模拟器中设置网络连接后解决了这个问题。
按照下面的步骤..
打开 VS Android 模拟器 >> 进入设置 >> 打开 WIFI >>
有 "Wired eth1" 个网络可用。
长按 "Wired eth1" 网络
然后 单击 >> 修改网络 >> 将 "IP Setting" 更改为 "Static" >>
更改网络的 IP 地址。
进行此设置后,我 解决了 我的问题,并从 REST API.
获得了回复谢谢。