vbnet网站获取天气预报网站后台数据

6613人阅读
android(101)
这个案例只是为了关联各个知识点,在实际开发中还有待优化
&项目结构分析:
Weather实体类:用来存放我们的天气实体
WeatherManager: 用来操作Weather
MainActivity:主acaitivy
CityWeatherService:定时轮询来更新前台的信息
原理比较简单直接贴出代码:
package com.example.
public class Weather
private String cityN
private String cityD
private String cityW
private String cityW
private String cityI
public Weather(){
public Weather(String cityName,String cityData,String cityWeath,String cityWinder,String cityImg){
this.cityName = cityN
this.cityData = cityD
this.cityWeath = cityW
this.cityWinder = cityW
this.cityImg = cityI
public String getCityName()
return cityN
public void setCityName(String cityName)
this.cityName = cityN
public String getCityData()
return cityD
public void setCityData(String cityData)
this.cityData = cityD
public String getCityWeath()
return cityW
public void setCityWeath(String cityWeath)
this.cityWeath = cityW
public String getCityWinder()
return cityW
public void setCityWinder(String cityWinder)
this.cityWinder = cityW
public String getCityImg()
return cityI
public void setCityImg(String cityImg)
this.cityImg = cityI
WeatherManager:
package com.example.
import org.json.JSONE
import org.json.JSONO
public class WeatherManager
public void setWeather(String jsonString){
JSONObject jsonObject = new JSONObject( jsonString );
JSONObject object = jsonObject.getJSONObject( &weatherinfo& );
String cityName = object.getString( &city& );
String cityData = object.getString( &date_y& );
String cityWeath = object.getString( &weather1& );
String cityWinder = object.getString( &wind1& );
String cityImg = object.getString( &img1& );
weather = new Weather( cityName, cityData, cityWeath, cityWinder, cityImg );
catch (JSONException e)
// TODO Auto-generated catch block
e.printStackTrace();
public Weather getWeather(){
CityWeatherService:
package com.example.
import java.io.ByteArrayOutputS
import java.io.InputS
import java.net.HttpURLC
import java.net.MalformedURLE
import java.net.URL;
import java.util.T
import java.util.TimerT
import android.app.S
import android.content.BroadcastR
import android.content.I
import android.os.H
import android.os.IB
import android.os.M
import android.util.L
public class CityWeatherService extends Service
private static final int UPDATAWEATHER = 0X10;
private final int GOTOBROADCAST = 0X20;
public static final String BROADCASTACTION = &com.jone.broad&;
public IBinder onBind(Intent arg0)
// TODO Auto-generated method stub
public int onStartCommand(Intent intent, int flags, int startId)
// updateWeather();
timer = new Timer();
timer.schedule( new TimerTask()
public void run()
// 定时更新
String jsonString = getWeather();
// 发送广播
Intent intent = new Intent();
intent.setAction( BROADCASTACTION );
intent.putExtra( &jsonstr&, jsonString );
sendBroadcast( intent );
Message msg = handler.obtainMessage();
msg.what = UPDATAWEATHER;
handler.sendMessage( msg );
}, 0, 20 * 1000 );
return super.onStartCommand( intent, flags, startId );
private String getWeather()
String srsString = &&;
srsString = getJsonStringGet( &.cn/data/.html& );
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();
Log.i( &tag&,e.getMessage() );
return srsS
public String getJsonStringGet(String uri) throws Exception
String result =
URL url = new URL( uri );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout( 6 * 1000 );// 设置连接超时
Log.i( &msg&, conn.getResponseCode() + &???????& );
if (conn.getResponseCode() == 200)
Log.i( &msg&, &成功& );
InputStream is = conn.getInputStream();// 得到网络返回的输入流
result = readData( is, &UTF-8& );
Log.i( &msg&, &失败& );
result = &&;
conn.disconnect();
private String readData(InputStream inSream, String charsetName) throws Exception
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inSream.read( buffer )) != -1)
outStream.write( buffer, 0, len );
byte[] data = outStream.toByteArray();
outStream.close();
inSream.close();
return new String( data, charsetName );
public void onDestroy()
// TODO Auto-generated method stub
super.onDestroy();
if(timer != null){
timer.cancel();
MainActivity:
package com.example.
import android.os.B
import android.os.H
import android.os.M
import android.app.A
import android.content.BroadcastR
import android.content.C
import android.content.I
import android.content.IntentF
import android.view.M
import android.widget.TextV
public class MainActivity extends Activity
BroadcastMain
broadcastM
public static WeatherM
TextView cityTextV
protected void onCreate(Bundle savedInstanceState)
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
cityTextView = (TextView) findViewById( R.id. city);
manager = new WeatherManager();
Intent intent = new Intent();
intent.setClass(this, CityWeatherService.class);
startService(intent);
broadcastMain = new BroadcastMain();
IntentFilter filter = new IntentFilter();
filter.addAction( CityWeatherService.BROADCASTACTION );
registerReceiver( broadcastMain, filter );
public boolean onCreateOptionsMenu(Menu menu)
// I this adds items to the action bar if it is present.
getMenuInflater().inflate( R.menu.main, menu );
public class BroadcastMain extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
String jsonString = intent.getExtras().getString( &jsonstr& );
manager.setWeather( jsonString );
Message msg = handler.obtainMessage();
msg.what = 01;
handler.sendMessage( msg );
Handler handler = new Handler()
public void handleMessage(android.os.Message msg)
switch (msg.what)
weather = manager.getWeather();
cityTextView.setText( weather.getCityName() );
protected void onDestroy()
Intent intent = new Intent();
intent.setClass(this, CityWeatherService.class);
stopService(intent);
super.onDestroy();
主要的功能实现在service中,开启一个定时器,去获取服务端的信息,通过广播来实时我们的activity中相关的组件
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:317177次
积分:3680
积分:3680
排名:第5753名
原创:74篇
转载:32篇
评论:108条
(1)(1)(2)(1)(3)(2)(1)(3)(3)(5)(4)(1)(2)(1)(6)(2)(3)(3)(10)(3)(7)(3)(2)(3)(2)(13)(8)(4)(4)(2)(1)1、C# asp.net 求获取天气预报的例子,可以在后台动态获取当天的天气信息,以文本或者XML格式等都可以。
我来回答:
登录后才能回答(&)
asp.net获取天气预报可以远程调用web服务,使用来源于中国气象局提供的实时天气的web服务,调用方法getWeatherbyCityName("城市名称"),可获取该城市的气象信息,其结果是一个字符串数组。
示例后台代码:
using System.D
using System.Collections.G
using System.W
using System.Web.UI;
using System.Web.UI.WebC
namespace MyWeather
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
DataSet mds = WeatherClass.getSupportDataSet();
if (!Page.IsPostBack)
DataTable dt = mds.Tables[0];//支持的省洲
this.Province.DataSource =
this.Province.DataValueField = "ID";//Value存储省洲代码
this.Province.DataTextField = "Zone";//显示省洲名称
this.Province.DataBind();
City.SelectedIndex = 1;
CityDataBind("1");//默认直辖市
GetWeatherByCode("54511");// 默认北京
catch (Exception)
Title.Text = "发现一个错误";
/// &summary&
/// 根据城市代码取得该城市的天气,并绑定到相关的显示控件
/// &/summary&
/// &param name="cityCode"&城市代码&/param&
protected void GetWeatherByCode(string cityCode)
String[] wa = WeatherClass.GetCityWeather(cityCode.Trim());//天气的字符串数组
Label1.Text = wa[10];
Label2.Text = wa[6] + "&&&" + wa[5] + "&&&" + wa[7];
Label3.Text = wa[13] + "&&&" + wa[12] + "&&&" + wa[14];
Label4.Text = wa[18] + "&&&" + wa[17] + "&&&" + wa[19];
Label5.Text = wa[11].Replace("\n", "&br /&");
Label6.Text = wa[22].Replace("\n", "&br /&");
Label7.Text = DateTime.Parse(wa[4]).ToString("yyyy年MM月dd日 dddd HH:mm");
Label8.Text = wa[0] + " / " + wa[1];
Image1.ImageUrl = "~/images/weather/" + wa[8];
Image2.ImageUrl = "~/images/weather/" + wa[9];
Image3.ImageUrl = "~/images/weather/" + wa[15];
Image4.ImageUrl = "~/images/weather/" + wa[16];
Image5.ImageUrl = "~/images/weather/" + wa[20];
Image6.ImageUrl = "~/images/weather/" + wa[21];
CityPhoto.ImageUrl = "http://www./tqyb/img/city/" + wa[3];
CityPhoto.AlternateText = City.SelectedItem.T
/// &summary&
/// 绑定城市的数据,根据省洲代码对显示进行筛选
/// &/summary&
/// &param name="zoneID"&省洲代码&/param&
protected void CityDataBind(string zoneID)
DataView dv = new DataView(WeatherClass.getSupportDataSet().Tables[1]);
dv.RowFilter = "[ZoneID] = " + zoneID;//筛选的条件是ZoneID=省洲代码
City.DataSource =
City.DataTextField = "Area";
City.DataValueField = "AreaCode";
City.DataBind();
City.Items.Insert(0, new ListItem("选择城市", "0"));
City.SelectedIndex = 0;
/// &summary&
/// 省洲选择变化的事件处理
/// &/summary&
/// &param name="sender"&&/param&
/// &param name="e"&&/param&
protected void Province_SelectedIndexChanged(object sender, EventArgs e)
CityDataBind(Province.SelectedItem.Value.Trim());
/// &summary&
/// 城市选择变化的事件处理
/// &/summary&
/// &param name="sender"&&/param&
/// &param name="e"&&/param&
protected void City_SelectedIndexChanged(object sender, EventArgs e)
if (City.Items[0].Value == "0")
City.Items.RemoveAt(0);
GetWeatherByCode(City.SelectedItem.Value.Trim());
扫描二维码关注微信
请选择(填写)你的举报理由
问题内容涉及敏感信息
问题广告嫌疑
散布谣言,扰乱社会秩序
侮辱或者诽谤他人,侵害他人合法权益
含有法律、行政法规禁止的其他内容MVC C#在后台接收一个气象台Json,在前台可以弹出json中所有的数据,但是现在想获取气象Json中每一个对象_百度知道
MVC C#在后台接收一个气象台Json,在前台可以弹出json中所有的数据,但是现在想获取气象Json中每一个对象
&/data/.ReadToEnd().ajax({
}这是前台的代码: &$,
strHTML = sr?这是controller中的代码.html" target="_blank">http、温度……)该怎么获取啊:
public JsonResult qixiang(string url)
{string strHTML = &quot://m, &#47?求大神帮助.
Stream myStream = myWebC
WebClient myWebClient = new WebClient().T如果要返回 json 这里很重要哦,
dataType.OpenRead(url);,
Home/script type=&&/
StreamReader sr = new StreamReader(myStream!;现在alert弹出了后台获取到的气象台的整个Json内容;Post&quot: function (result) {
alert(result).html&quot!详细说明参见JQuery的API手册吧
JsonResult js = new JsonResult();&url=&
/data/.GetEncoding(&quot:&
js!,但是现在想获取气象Json中每一个对象(日期,但是怎么显示出Json中每一个元素啊MVC C#在后台接收一个气象台J;;http:result = eval(&.html”};json&#39,也可以转换一下。你写的应该是没有什么问题的;Home&#47://m.cn/data/, function (data) {
alert(data); 试试;);)&};;或者是result = $.parseJSON(result);;&#47.post(&quot:“<a href=")://m, &#39
现在获取的是{&weatherinfo&:{&city&:&北京&,&city_en&:&beijing&,&date_y&:&日&……}}这个Json对象,但是我想让他显示为:城市:北京
星期:周五 ……
是显示不出来啊
var theCityInfor = result.alert(&城市:& + theCityInfor.city + &
日期:&+theCityInfor.date_y + &...&);这个需要自己解析拼接,你也可以自己写一个方法来解析,毕竟人家的AIP是固定的。
这个方法试过了但是就是没结果,现在的var theCityInfor = result.“theCityInfor”为空获取不到theCityInfor.city的值
var obj = {&weatherinfo&:{&city&:&北京&,&city_en&:&beijing&,&date_y&:&日&}};alert(obj.weatherinfo.city);这样是可以获取到的 alert(obj) 弹出来的是 objct。你要确定alert(result)出来的是object而不是{&weatherinfo&:{&city&:&北京&,&city_en&:&beijing&,&date_y&:&日&……}}字符串。
弹出来的是{&weatherinfo&:{&city&:&北京&,&city_en&:&beijing&,&date_y&:&日&……}}字符串。
解析一下吧,用上面给你说的方法,解析成Object。
提问者评价
谢谢提醒,现在问题已经解决啦!非常感谢
其他类似问题
为您推荐:
json的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁您所在位置: &
&nbsp&&nbsp&nbsp&&nbsp
毕业设计(论文)-基于Android的气象信息采集系统.doc39页
本文档一共被下载:
次 ,您可免费全文在线阅读后下载本文档
文档加载中...广告还剩秒
需要金币:200 &&
你可能关注的文档:
··········
··········
毕业论文 设计
目 基于Android的气象信息采集系统
计算机与软件学院
计算机科学与技术
二O一一 年 五 月二十日
本人郑重声明:
持以“求实、创新”的科学精神从事研究工作。
本论文是我个人在导师指导下进行的研究工作和取得的研究成果。
本论文中除引文外,所有实验、数据和有关材料均是真实的。
本论文中除引文和致谢的内容外,没有抄袭其他人或其他机构已经发表或撰写过的研究成果。
其他同志对本研究所做的贡献均已在论文中作了声明并表示了谢意。
作者签名:
1.1 项目背景 1
1.2 项目意义 1
2 核心技术简介 2
2.1 Android平台框架 2
2.2 Android的消息机制 3
2.2.1 与消息机制有关的几个类 3
2.2.2 消息机制的实现原理 4
2.3 Android XML解析技术分析 5
2.3.1 DOM解析技术 5
2.3.2 SAX解析技术 6
2.3.3 XML pull 解析技术 7
3 系统开发工具 7
3.1 MyEclipse 7
3.2 Android SDK 8
3.3 集成开发环境的搭建 8
3.3.1 JAVA
JDK的安装配置 8
3.3.2 Android SDK的配置 10
3.3.3 ADT Plugin for Eclipse 10
4 系统设计 11
4.1系统概述 11
4.2系统需求分析 11
4.3功能模块设计 12
5 代码实现 12
5.1系统的命名空间 12
5.2主界面实现 13
5.3天气查询 16
5.4信息员登录 19
5.5个人信息 20
5.6信息反馈 21
5.7培训信息 26
5.8手机定位 27
5.9系统设置 29
5.10发送天气信息 31
ABSTRACT II
基于Android的气象信息采集系统
南京信息工程大学计算机与软件学院 南京 210044
摘要:本系统主要用于各地的信息人员获取、上传和采集当地气象信息,使得服务器端人员能够根据信息
正在加载中,请稍后...&#xe621; 上传我的文档
&#xe602; 下载
&#xe60c; 收藏
该文档贡献者很忙,什么也没留下。
&#xe602; 下载此文档
正在努力加载中...
基于Android的天气预报查询系统的实现毕业设计
下载积分:1500
内容提示:基于Android的天气预报查询系统的实现毕业设计
文档格式:DOC|
浏览次数:9|
上传日期: 13:31:32|
文档星级:&#xe60b;&#xe612;&#xe612;&#xe612;&#xe612;
该用户还上传了这些文档
基于Android的天气预报查询系统的实现毕业设计
官方公共微信

我要回帖

更多关于 怎么获取网站后台地址 的文章

 

随机推荐