【问题标题】:xamarin android async method not workingxamarin android异步方法不起作用
【发布时间】:2016-04-18 14:19:50
【问题描述】:

我的可移植类上有一个静态方法

async public static Task<JSONObject> CheckToken(string token)
{
    var client = new HttpClient();
    var formContent = new FormUrlEncodedContent(new[]
   {
        new KeyValuePair<string, string>("tag", "checkToken"),
        new KeyValuePair<string, string>("token", token)
    });
    var response = await client.PostAsync("http://10.0.0.6:888/api.php",     formContent);
    string res = await response.Content.ReadAsStringAsync();
    return new JSONObject(res);
}

主要活动代码

protected override async void OnCreate (Bundle bundle)
{
    FacebookSdk.SdkInitialize(this);

    LoginManager.Instance.LogOut();

    base.OnCreate(bundle);
    var tracker = new myTokenT(this);
    tracker.StartTracking();

    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
    var token = prefs.GetString("token","notoken");
    var res = await Client.CheckToken(token); // stop here
......
}

当它到达时

 var response = await client.PostAsync("http://10.0.0.6:888/api.php",     formContent);

出现异常 System.Threading.Tasks.TaskCanceledException:任务已取消。

它停止了......就像我等了 3 分钟然后我得到了异常, 有趣的是它以前工作过!我不知道为什么会这样。

【问题讨论】:

  • 10.0.0.6:888/api.php 是否可以从您正在测试它的设备或模拟器访问?
  • 是的,它是我计算机的内部 ip,我还检查了手动发送请求并且工作正常
  • 你可以使用设备/模拟器的浏览器访问“10.0.0.6”吗?使用 adb:adb shell ping 10.0.0.6 来测试从设备/模拟器内部对服务器的访问。

标签: c# android asynchronous xamarin async-await


【解决方案1】:
  • 假设在该服务器上启用了 ICMP,使用 Android 的 adb 测试从设备/模拟器内部对该服务器的访问:

    adb shell ping 10.0.0.6

  • 假设您在该 Web 服务器上有某种类型的开放访问测试/状态网页,您可以从设备/模拟器的 Web 浏览器浏览该服务器吗?

    李>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2018-09-12
    • 2012-03-04
    • 2018-08-04
    • 2014-01-20
    • 1970-01-01
    相关资源
    最近更新 更多