java web天气_java web service实现天气预报功能

展开全部

前台js界面代码://省份

function LoadProvince() {

$.ajax({

type: "POST",

url: "ashx/weatherHandler.ashx",

data: "option=province",

success: function (result) {

$(".sel-province option").remove();

var arry = result.split('|');

var obj = null;

for (var i = 0; i 

if (arry[i] !e5a48de588b662616964757a686964616f31333337396166= null && arry[i] != "") {

obj = arry[i].split(',');

$(".sel-province").append("" + obj[0] + "");

}

}

$(".sel-province").find("option[text='北京']").attr("selected", "selected");

},

error: function (errorMsg) {

$(".result-table tr").remove();

$(".result-table").append("

省份请求出现错误,请您稍后重试。。。");

}

});

}

//城市

function LoadCity(provinceid) {

$.ajax({

type: "POST",

url: "ashx/weatherHandler.ashx",

data: "provinceid=" + provinceid + "&option=city",

success: function (result) {

$(".sel-city option").remove();

var arry = result.split('|');

var obj = null;

for (var i = 0; i 

if (arry[i] != null && arry[i] != "") {

obj = arry[i].split(',');

$(".sel-city").append("" + obj[0] + "");

}

}

},

error: function (errorMsg) {

$(".result-table tr").remove();

$(".result-table").append("

城市请求出现错误,请您稍后重试。。。");

}

});

}

//加载天气

function GetWeather(cityid) {

$.ajax({

type: "POST",

url: "ashx/weatherHandler.ashx",

data: "cityid=" + cityid + "&option=weather",

success: function (result) {

$(".result-table tr").remove();

var arry = result.split('|');

var obj = null;

for (var i = 0; i 

if (arry[i] != null && arry[i] != "") {

if (arry[i].indexOf(".gif") > 0) {

$(".result-table").append("

");

}

else {

$(".result-table").append("

" + arry[i] + "");

}

}

}

},

error: function (errorMsg) {

$(".result-table tr").remove();

$(".result-table").append("

天气数据请求出现错误,请您稍后重试。。。");

}

});

}

html代码:

由于js不支持跨域,直接ajax+ashx一般处理程序(在里面调用天气接口)。一般处理程序代码如下:using System.Web;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Text;

namespace WeatherTest.ashx

{

/// 

/// weatherHandler 的摘要说明

/// 

public class weatherHandler : IHttpHandler

{

WeatherWsClient.WeatherWSSoapClient client = new WeatherWsClient.WeatherWSSoapClient();

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType = "text/plain";

string[] result = null;

string option = context.Request.Form["option"];

switch (option)

{

case "province":

result = GetProvinces();

break;

case "city":

result = GetCitys(context.Request.Form["provinceid"]);

break;

case "weather":

result = GetWeather(context.Request.Form["cityid"], null);

break;

}

string str = ConvertToString(result, option);

context.Response.Write(str);

}

/// 

/// 数组转字符串

/// 

/// 

/// 

/// 

private string ConvertToString(string[] result, string option)

{

StringBuilder sb = new StringBuilder();

foreach (string item in result)

{

sb.Append(item+"|");

}

return sb.ToString();

}

/// 

/// 省份

/// 

/// 

private string[] GetProvinces()

{

return client.getRegionProvince();

}

/// 

/// 城市

/// 

/// 

/// 

private string[] GetCitys(string provinceid)

{

return client.getSupportCityString(provinceid);

}

/// 

/// 天气数据

/// 

/// 

/// 

/// 

private string[] GetWeather(string cityid, string userid)

{

return client.getWeather(cityid, userid);

}

public bool IsReusable

{

get

{

return false;

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值