Xamarin Watch - 使用 Web 服务 (Android)
Xamarin Watch - Consume web service (Android)
我正尝试从我的 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 "";
}
}
你有什么想法吗?
谢谢,杰彭
经过大量研究后,我找到了解决方案,感谢: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();
Jeppen,祝你编码愉快
我正尝试从我的 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 "";
}
}
你有什么想法吗?
谢谢,杰彭
经过大量研究后,我找到了解决方案,感谢: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();
Jeppen,祝你编码愉快