Jeppen
Jeppen

Reputation: 424

Xamarin Watch - Consume web service (Android)

I'm trying to call a website from my android watch but the thread exits (The thread 0x5 has exited with code 0 (0x0)) without a result. I added the permissions "Internet" and "Network_state", which does not change the result. Below my code (done in Tizen and pure 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 "";
        }
    }

Do you have any idea?

Thanks, Jeppen

Upvotes: 0

Views: 187

Answers (1)

Jeppen
Jeppen

Reputation: 424

After quite some research I found the solution thanks to: https://github.com/Samsung/Tizen-CSharp-Samples/tree/master/Wearable

Give the app the following permissions:

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

C# code

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();

Happy coding, Jeppen

Upvotes: 1

Related Questions