【问题标题】:Xamarin & RestSharp - Async call hangs/never returnsXamarin & RestSharp - 异步调用挂起/从不返回
【发布时间】:2014-09-29 19:06:23
【问题描述】:

我正在 Xamarin 中编写一个 Android 应用程序,以使用 restsharp 执行异步 json 调用。我的设置涉及一个 android java 绑定库(转换为 c# 的 jar 文件)。这个库有一个类,它是 Application 的子类,看起来像这样

HealthData.java(它是 jar 的一部分并作为项目引用到应用程序中)意图是在加载任何活动之前运行它。

import android.app.Application;

public class HealthData extends Application {
    private int intTest;

    HealthData () {
        intTest = 0;
    }

    public int getIntTest(){
        return intTest;
    }

    public void setIntTest(int value){
        intTest = value;
    }
}

我正在 AndroidManifest.xml 中加载这个子类,如下所示 -

<application android:label=“demo" android:name=“.HealthData">
        <activity android:name=“.MainActivity" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
    </application>

而我执行异步调用的 mainactivity(用 c# 编写)看起来像这样 -

using RestSharp;

namespace demo
{
    [Activity (Label = "demo", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        static string TAG = "PRINT-ME";

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            callme();

            Log.Info (TAG, "callme returns");
        }

        private async void callme()
        {
            // Call the method that runs asynchronously.
            string result = await WaitAsynchronouslyAsync();

            // Display the result.
            TextView txt = FindViewById<TextView> (Resource.Id.textViewAsync);
            txt.Text = result;
        }

        public async Task<string> WaitAsynchronouslyAsync()
        {
            // fetch some json via restsharp
            var client = new RestClient ("http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/");
            var request = new RestRequest (String.Format ("{0}/allinfo", "198440"));

            Log.Info (TAG, "before awaiting");

            // ExecuteTaskSync
            IRestResponse resp = await ExecuteTaskAsync (client, request);

            return resp.Content;
        }
}

问题 - 当我在清单文件中使用 HealthData 时,ExecuteTaskAsync 阻塞/从不返回。如果我跳过在 AndroidManifest.xml 中加载 HealthData,异步调用会返回,一切都很好。

知道为什么我使用 HealthData 时异步调用会失败吗?我没有看到来自 logcat 的任何错误或警告。

我已尝试使用 C# 版本的 HealthData 并在 Manifest.xml 中使用它,并且异步调用工作正常。

【问题讨论】:

    标签: android android-asynctask xamarin restsharp c#-5.0


    【解决方案1】:

    因为 callme 返回 void 而不是返回 Task,所以 callme 中发生的任何异常都会被吃掉并且永远不会冒泡,所以这可能就是你永远看不到它完成的原因。

    试试这个:

    // Mark OnCreate as 'async'
    protected async override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
    
        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
    
        var callMeTask = callme();
        await callMeTask;
    
        if (callMeTask.IsFaulted) {
            Log.Info(TAG, "callMe Failed with exception: " + callMeTask.Exception);
        } else {
            Log.Info (TAG, "callme returns");
        }
    }
    
    // Have callMe return Task
    private async Task callme()
    {
        // No changes
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 2015-11-23
      • 2017-05-28
      • 1970-01-01
      相关资源
      最近更新 更多