Windows Phone 8.1的Web服务调用

时间:2016-04-17 04:24:28

标签: c# rest visual-studio-2015 windows-phone-8.1

我对windows phone app开发完全陌生。我刚刚安装了Visual Studio 2015并开始使用新的应用程序。一直顺利,直到到达我的后台办公室的网络服务。首先,我无法调试代码抛出异常的原因和位置。然后我意识到并尝试将异常消息写入文本块。 注意我尝试使用与Googling找到的工作示例相同的代码。

   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;   
    }            
}

2 个答案:

答案 0 :(得分:0)

您使用动态类型为您提供了预期

dynamic resultObj = "";

这使得resultObj成为一个字符串

resultObj = Newtonsoft.Json.Linq.JObject.Parse(response);

你试着坚持下去。我建议你将响应解析为一个类型。

答案 1 :(得分:0)

试试这个

   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";
        }
    }