无法从xamarin表单的Web服务检索响应

时间:2016-10-18 02:49:34

标签: c# web-services xaml visual-studio-2015 xamarin.forms

您好我是编程的新手,我仍然是一名学生正在尝试学习C#而我应该从Web服务中检索响应,但我不能并且它无法同时输入if和else语句。如何解决此错误

.xaml.cs

private async void GetData(object sender, EventArgs e)
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://172.20.129.44/");
    // Add an Accept header for JSON format.
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    //try
    //{
    HttpResponseMessage response = client.GetAsync("WebServices/information.svc/GetInformationJSON").Result;
    if (response.IsSuccessStatusCode)
    {
        string jsonString = await response.Content.ReadAsStringAsync();
        dynamic dynamicObject = JsonConvert.DeserializeObject(jsonString);
        //string abc =  dynamicObject["infoOpeningDays"].ToString();
        // List<String[]> dynamicObject = JsonConvert.DeserializeObject<List<String[]>>(jsonString);
        //Debug.WriteLine(dynamicObject[0].ToString());
        //string abc = dynamicObject.IEnumerator.[0].IEnumerator.[0].IEnumerator.[0].IEnumerator.[5].Name;
        try
        {
            //  var abc = dynamicObject.GetType().GetProperty("infoOpeningDays").GetValue(dynamicObject);
            String abc = (String)dynamicObject["infoOpeningDays"];
            //JArray v = new JArray();
            //v[0].data,tostring()
        }
        catch (Exception ex) { }
        //Debug.WriteLine(abc.ToString());
        // Debug.WriteLine((string)abc);
    }
    else
    {
        Debug.WriteLine("It entered else not if");
    }
    //}
    //catch (Exception ex)
    //{
    //    Debug.WriteLine(ex.ToString());
    //}
}

的.aspx

<Button Text="Get Data" TextColor="White" BackgroundColor="#4282bd" Clicked="GetData"/>

网络服务

http://172.20.129.44/WebServices/information.svc/GetInformationJSON

网络服务数据

{
    "d": [
        {
            "__type": "Info:#website.Model",
            "infoClosingDays": "Friday",
            "infoClosingHours": "06:00:00 PM",
            "infoID": 1,
            "infoOpeningDays": "Monday",
            "infoOpeningHours": "09:30:00 AM",
            "infoStatus": "Open"
        }
    ]
}

有人请救救我

1 个答案:

答案 0 :(得分:0)

您可以尝试使用

class func getHourlyWeather(searchString:String,completionHandler:([HourlyTemperatures])-> ()){


    var hourTemperatureArray = [HourlyTemperatures]()


     let hourtempObj = HourlyTemperatures(hourlyTemperatureInF:34 , hourlyTime: 2, hourlyIcon: sunny, hourlyAmPm: 4) as AnyObject

     hourTemperatureArray.append(hourtempObj)




            } catch let error as NSError {
                print("error processing json data: \(error.localizedDescription)")
            }
            completionHandler(hourTemperatureArray)
        }
        else{
            completionHandler(hourTemperatureArray)
            print("Error details:: \(error!.localizedDescription)")
        }

    })
    task.resume()

}

这可能会解决您的问题。