• 注册
当前位置:1313e > 默认分类 >正文

asp.net MVC中实现调取web api

  1  public ActionResult Index(string city)
  2         {
  3 
  4 
  5             if (string.IsNullOrEmpty(city))
  6             {
  7                 city = "上海";
  8             }
  9 //ak需要自己到百度地图官网申请
 10 Uri weatherInfo = new Uri("http://api.map.baidu.com/telematics/v3/weather?location=" + city + "&output=json&ak=UKMGHnstHCOFzYBe2h70gi5fLsc0C0dG");
 11 
 12    HttpWebRequest request = WebRequest.Create(weatherInfo) as HttpWebRequest;
 13             List weatherDataList = new List();
 14 
 15             using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
 16             {
 17                 // Get the response stream
 18                 StreamReader reader = new StreamReader(response.GetResponseStream());
 19                 string json = reader.ReadToEnd();
 20 
 21                 Weather model = JsonHelper.JsonToEntity(json);
 22                 List weatherDtoList = new List();
 23                 List indexList = new List();
 24 
 25                 foreach (WeatherResult result in model.Results)
 26                 {
 27                     indexList = result.Index;
 28                     weatherDataList = result.Weather_Data;
 29                 }
 30                 for (int i = 0; i < indexList.Count; i++)
 31                 {
 32                     WeatherDto weatherDto = new WeatherDto();
 33                     weatherDto.Des = indexList[i].Des;
 34                     weatherDto.Tipt = indexList[i].Tipt;
 35                     weatherDto.Title = indexList[i].Title;
 36                     weatherDto.Zs = indexList[i].Zs;
 37 
 38                     if (i < weatherDataList.Count)
 39                     {
 40                         weatherDto.Date = weatherDataList[i].Date;
 41                         weatherDto.DayPictureUrl = weatherDataList[i].DayPictureUrl;
 42                         weatherDto.NightPictureUrl = weatherDataList[i].NightPictureUrl;
 43                         weatherDto.Temperature = weatherDataList[i].Temperature;
 44                         weatherDto.Weather = weatherDataList[i].Weather;
 45                         weatherDto.Wind = weatherDataList[i].Wind;
 46 
 47                     }
 48                     weatherDtoList.Add(weatherDto);
 49                 }
 50             }
 51 
 52             return View("NewView",weatherDataList);
 53         }
 54 
 55     }
 56 
 57  public class WeatherDto
 58     {
 59         public string Title { get; set; }
 60 
 61         public string Zs { get; set; }
 62 
 63         public string Tipt { get; set; }
 64 
 65         public string Des { get; set; }
 66 
 67         public string Date { get; set; }
 68 
 69         public string DayPictureUrl { get; set; }
 70 
 71         public string NightPictureUrl { get; set; }
 72 
 73         public string Weather { get; set; }
 74 
 75         public string Wind { get; set; }
 76 
 77         public string Temperature { get; set; }
 78     }
 79 
 80 
 81  public class Weather
 82     {
 83         public string Error { get; set; }
 84 
 85         public string Status { get; set; }
 86 
 87         public string Date { get; set; }
 88 
 89         public List Results { get; set; }
 90     }
 91 
 92  public class WeatherResult
 93     {
 94         public string CurrentCity { get; set; }
 95 
 96         public string Pm25 { get; set; }
 97 
 98         public List Index { get; set; }
 99 
100         public List Weather_Data { get; set; }
101     }
  1 @model IEnumerable
  2 
  3 @{
  4     Layout = null;
  5 }
  6 
  7 
  8 
  9     weather
 10     "shortcut icon" href="http://p8.qhimg.com/t0158c24c5ddb3a6745.png" type="image/x-icon">
 11     "stylesheet" href="~/Content/css/Weather.css" />
 12     
 70     
 71     
 88 
 89 
 90 class="skin6" style="display: block;">
 91     
92 <select id="GetCity" οnchange="Change()"> 93 94 95 96 97 98 select> 99
100
class="morewether" id="partRefresh"> 101 102
    "foreast"> 103
  • 104

    class="colora">@Model.ToList()[0].Date

    105

    class="icon-tu">class="icon-qing">
    @Model.ToList()[0].Weather

    106

    class="otherinfo">@Model.ToList()[0].Temperature@Model.ToList()[0].Wind

    107
  • 108
  • 109

    class="colora">@Model.ToList()[1].Date

    110

    class="icon-tu">class="icon-shachen">
    @Model.ToList()[1].Weather

    111

    class="otherinfo">@Model.ToList()[1].Temperature@Model.ToList()[1].Wind

    112
  • 113
  • 114

    class="colora">@Model.ToList()[2].Date

    115

    class="icon-tu">class="icon-shachen">
    @Model.ToList()[2].Weather

    116

    class="otherinfo">@Model.ToList()[2].Temperature@Model.ToList()[2].Wind

    117
  • 118
  • 119

    class="colora">@Model.ToList()[3].Date

    120

    class="icon-tu">class="icon-shachen">
    @Model.ToList()[3].Weather

    121

    class="otherinfo">@Model.ToList()[3].Temperature@Model.ToList()[3].Wind

    122
  • 123
124
125 126

 效果图:

转载于:https://www.cnblogs.com/kejie/p/6644751.html

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录