Web 服务调用 Windows Phone 8.1
Web Service Call for Windows Phone 8.1
我完全不熟悉 windows phone 应用程序开发。我刚刚安装了 Visual Studio 2015 并开始开发新应用程序。一切顺利,直到调用我后台的 Web 服务的时间到了。首先,我无法调试代码抛出异常的原因和位置。然后我意识到并尝试将异常消息写入文本块。
请注意,我尝试使用与谷歌搜索找到的工作示例中获得的相同代码。
Error: "HRESULT E_FAIL has been returned from a call to a COM component"
代码:
public async Task<dynamic> getHomeCategories()
{
string url = string.Format("my working api url");
var uri = new Uri(url);
var client = new HttpClient();
dynamic resultObj = "";
//using (HttpResponseMessage response = await client.GetAsync(url))
try
{
var response = await client.GetStringAsync(uri);
resultObj = Newtonsoft.Json.Linq.JObject.Parse(response);
wsresult.Text = "okay";
return resultObj;
}
catch(Exception Ex) {
wsresult.Text = Ex.Message.ToString(); return resultObj;
}
}
这是你对动态类型的使用给了你预期
dynamic resultObj = "";
这使得 resultObj 成为一个字符串
resultObj = Newtonsoft.Json.Linq.JObject.Parse(response);
您尝试在其中粘贴一个 class。我建议您将响应解析为一种类型。
试试这个
HttpResponseMessage response;
public async Task<string> webserviceResponse(string HttpMethod)
{
// check internet connection is available or not
if (NetworkInterface.GetIsNetworkAvailable() == true)
{
// CancellationTokenSource cts = new CancellationTokenSource(2000); // 2 seconds
HttpClient client = new HttpClient();
MultipartFormDataContent mfdc = new MultipartFormDataContent();
mfdc.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
string GenrateUrl = "your url";
if (HttpMethod == "POST")
{
response = await client.PostAsync(GenrateUrl, mfdc);
}
else if (HttpMethod == "PUT")
{
response = await client.PutAsync(GenrateUrl, mfdc);
}
else if (HttpMethod == "GET")
{
response = await client.GetAsync(GenrateUrl);
}
var respon = await response.Content.ReadAsStringAsync();
string convert_response = respon.ToString();
return convert_response;
}
else
{
return "0";
}
}
我完全不熟悉 windows phone 应用程序开发。我刚刚安装了 Visual Studio 2015 并开始开发新应用程序。一切顺利,直到调用我后台的 Web 服务的时间到了。首先,我无法调试代码抛出异常的原因和位置。然后我意识到并尝试将异常消息写入文本块。 请注意,我尝试使用与谷歌搜索找到的工作示例中获得的相同代码。
Error: "HRESULT E_FAIL has been returned from a call to a COM component"
代码:
public async Task<dynamic> getHomeCategories()
{
string url = string.Format("my working api url");
var uri = new Uri(url);
var client = new HttpClient();
dynamic resultObj = "";
//using (HttpResponseMessage response = await client.GetAsync(url))
try
{
var response = await client.GetStringAsync(uri);
resultObj = Newtonsoft.Json.Linq.JObject.Parse(response);
wsresult.Text = "okay";
return resultObj;
}
catch(Exception Ex) {
wsresult.Text = Ex.Message.ToString(); return resultObj;
}
}
这是你对动态类型的使用给了你预期
dynamic resultObj = "";
这使得 resultObj 成为一个字符串
resultObj = Newtonsoft.Json.Linq.JObject.Parse(response);
您尝试在其中粘贴一个 class。我建议您将响应解析为一种类型。
试试这个
HttpResponseMessage response;
public async Task<string> webserviceResponse(string HttpMethod)
{
// check internet connection is available or not
if (NetworkInterface.GetIsNetworkAvailable() == true)
{
// CancellationTokenSource cts = new CancellationTokenSource(2000); // 2 seconds
HttpClient client = new HttpClient();
MultipartFormDataContent mfdc = new MultipartFormDataContent();
mfdc.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
string GenrateUrl = "your url";
if (HttpMethod == "POST")
{
response = await client.PostAsync(GenrateUrl, mfdc);
}
else if (HttpMethod == "PUT")
{
response = await client.PutAsync(GenrateUrl, mfdc);
}
else if (HttpMethod == "GET")
{
response = await client.GetAsync(GenrateUrl);
}
var respon = await response.Content.ReadAsStringAsync();
string convert_response = respon.ToString();
return convert_response;
}
else
{
return "0";
}
}