【问题标题】:Xamarin Watch - Consume web service (Android)Xamarin Watch - 使用 Web 服务 (Android)
【发布时间】:2020-03-17 20:27:36
【问题描述】:

我正在尝试从我的 android 手表调用网站,但线程退出(线程 0x5 已退出,代码 0 (0x0))没有结果。 我添加了权限“Internet”和“Network_state”,这不会改变结果。在我的代码下方(在 Tizen 和纯 Xamarin 中完成):

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.activity_main);

        textView = FindViewById<TextView>(Resource.Id.text);
        Button mybtn = FindViewById<Button>(Resource.Id.btnCal);
        SetAmbientEnabled();

        try
        {
            mybtn.Click += delegate
            {
                Task<string> callTask = calculateRoute();
                callTask.Wait();
                string astr = callTask.Result;
            };
        }
        catch (Exception ex)
        {
            string tt = ex.ToString();
        }
    }

    private async Task<string> calculateRoute()
    {
        HttpClient client;
        try
        {
            String RestUrl = "https://www.google.com";
            var uri = new Uri(string.Format(RestUrl, string.Empty));

            client = new System.Net.Http.HttpClient();

            var response = await client.GetAsync(uri);
            var content = await response.Content.ReadAsStringAsync();

            return content;
        }
        catch (Exception ex)
        {
            string tt = ex.ToString();
            return "";
        }
    }

你有什么想法吗?

谢谢,杰彭

【问题讨论】:

  • 我没有使用 Tizen 可穿戴设备,但是用 async 替换您的任务等待/结果以防止死锁。

标签: android xamarin tizen-wearable-sdk android-wear-2.0


【解决方案1】:

经过大量研究,我找到了解决方案,感谢:https://github.com/Samsung/Tizen-CSharp-Samples/tree/master/Wearable

为应用授予以下权限:

<privilege>http://tizen.org/privilege/internet</privilege>
<privilege>http://tizen.org/privilege/network.get</privilege>
<privilege>http://tizen.org/privilege/download</privilege>

C#代码

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.google.lu");

               //get the data
                request.Method = "GET";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                string tt = ((HttpWebResponse)response).StatusDescription + "\n";
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();

快乐的编码,杰彭

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多