Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android、Xamarin形成PCL、PortableRest PCL和异步Web Api调用_Android_Xamarin_Async Await_Xamarin.forms - Fatal编程技术网

Android、Xamarin形成PCL、PortableRest PCL和异步Web Api调用

Android、Xamarin形成PCL、PortableRest PCL和异步Web Api调用,android,xamarin,async-await,xamarin.forms,Android,Xamarin,Async Await,Xamarin.forms,我试图使用从Xamarin表单对Web API 2.2 Rest服务进行异步调用 我想我有一些死锁/同步上下文问题,但我无法解决(新手到异步等) 有人能帮忙吗 我的控制器测试方法(删除了对数据库的任何调用)- 我的PortableRest代理(PCL)方法- wait to api.TestRest()不会导致设置HeaderLabel.After或ListView 如果我只是添加了一个测试代理方法GetDelay(),它不会通过return await ExecuteAsync>(请求)调用P

我试图使用从Xamarin表单对Web API 2.2 Rest服务进行异步调用

我想我有一些死锁/同步上下文问题,但我无法解决(新手到异步等)

有人能帮忙吗

我的控制器测试方法(删除了对数据库的任何调用)-

我的PortableRest代理(PCL)方法-

wait to api.TestRest()不会导致设置HeaderLabel.After或ListView

如果我只是添加了一个测试代理方法GetDelay(),它不会通过return await ExecuteAsync>(请求)调用PortableRest

public async Task<bool> GetDelay ()
{
    await Task.Delay (1000);
    return true;
}
公共异步任务GetDelay() { 等待任务。延迟(1000); 返回true; } 然后所有的“工作”


感谢您的帮助,如果需要,我将下载PortableRest的源代码(目前使用Nuget dll),只是不确定是否缺少此顶级的基本功能。

对,这最终与异步无关(或PortableRest的任何问题),但我想我会与您分享,以防它能帮助其他人

我最终放置了一个try块(显然是下一步!)并捕获了这个异常-

未找到方法:“System.Net.Http.HttpClientHandler.set\u AutomaticDecompression”。

那么,看看这个-

PortableRest已经在我的Forms PCL中,但我需要将Microsoft Http客户端库Nuget添加到我的Android应用程序中

然后我得到了-

错误:连接失败(请求的地址在此上下文中无效)

我认为这可能与Android权限有关,但事实并非如此(尽管我现在意识到我需要为未来的通话添加互联网权限)

因为我正在虚拟机中运行我的应用程序(从GenyMotion/Virtual Box),所以Android应用程序无法访问LocalHost(我的Web Api所在的位置)

那么从这个-

我可以确认,使用VirtualBox>File>Preferences>Network>VirtualBox Host Only Network Adapter>Modify中显示的地址可以很好地完成工作


谢谢您的帮助。

您检查过acutal网络流量了吗?连接时可能发生错误。您也可以尝试使用HttpClient.GetAsync来解决连接问题Hanks@Stenprov,我检查了流量,您是对的,没有向外拨打电话,我现在在try块中包围了该电话,并进一步查看
[TestMethod]
public async Task TestRest()
{
    MyNewsApiClient MyNewsApiClient = new MyNewsApiClient();

    var models = await MyNewsApiClient.TestRest();
    int count = models.Count;
    Assert.AreEqual(1, count);
}
public async Task<List<ContentModel>> TestRest()
{
    var request = new RestRequest();
    request.Resource = "Content/GetTestRest";

    return await ExecuteAsync<List<ContentModel>>(request);
}
public partial class NewsType1CP : ContentPage
{
    public NewsType1CP ()
    {
        InitializeComponent ();
    }

    protected override void OnAppearing ()
    {
        LoadData ();  // this is a sync call of an async method, not sure how else to  approach, make OnAppearing async?
    }

    public async Task LoadData ()
    {
        Debug.WriteLine ("LoadData");

        HeaderLabel.Text = "Load Data!";

        MyNewsApiClient api = new MyNewsApiClient ();

        var cm = await api.TestRest ();
        // this will work on its own, control returns to this method - await api.GetDelay ();

        HeaderLabel.Text = "After! - ";

        NewsListView.ItemsSource = cm;
    }
}
public async Task<bool> GetDelay ()
{
    await Task.Delay (1000);
    return true;
}