访问中国天气网获取最近三天的数据
来源:互联网 发布:php内核源码分析 编辑:程序博客网 时间:2024/04/30 14:35
首先上一个效果图
这是本人在一款app中所用到的数据,获取最近三天中国天气网上的数据。
怎么说呢中国天气网上的数据比较乱,但是相比其他网站上的数据,中国天气网上的数据还是比较稳定的。
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx 这个就是接口地址,上面不仅有天气的数据还有国家和城市的数据
可以采用webservice方式调用接口,也可以用java代码调用接口,我是采用java代码调用天气接口的
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather 天气预报接口地址
我是采用post方式调的接口
大家注意一下中国天气网上传过来的数据是一个一维数据,所以处理这个字符串就比较麻烦
这是我访问地址以及处理数据的代码
private void getWeather(String theCityName) {
UIHelper.showDialogForLoading(getActivity(), "加载数据请稍后...", true);
wts.clear();
FormBody.Builder builder = new FormBody.Builder();
builder.add("theCityName", theCityName);
RequestBody formBody = builder.build();
final Request request = new Request.Builder().url(Contant.weatherurl)
.post(formBody).build();
client.newCall(request).enqueue(callback2);
}
// 请求后的回调方法
private Callback callback2 = new Callback() {
@Override
public void onFailure(Call call, IOException e) {
setResult2(e.getMessage(), false);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
setResult2(response.body().string(), true);
}
};
// 显示请求返回的结果
private void setResult2(final String msg, final boolean success) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (success) {
UIHelper.hideDialogForLoading();
System.out.println("msg::" + msg);
// InputStream input =new new
// ByteArrayInputStream(str.getBytes("UTF-8"))
try {
weather = (ArrayList<String>) XmlPull
.getWeather(new ByteArrayInputStream(msg
.getBytes("UTF-8")));
// for(int i=0;i<weather.size();i++){
// System.out.println("i="+weather.get(i));
String weather1 = weather.get(6);
String weather2 = weather.get(5);
String weather3 = weather.get(7);
String weather4 = weather.get(8);
System.out.println("weather4="+weather4);
String[] weather11 = weather1.split(" ");
String[] weather44 = weather4.split("\\.");
//System.out.println("weather11="+weather11[0]+",weather11="+weather11[1]);
//System.out.println("weather44="+weather44[0]+",weather44="+weather44[1]);
Class<com.mtkj.scpublish.R.drawable> cls = R.drawable.class;
int value1 = cls.getDeclaredField("a_" + weather44[0])
.getInt(null);
WTEntity wt1 = new WTEntity(value1, weather11[0],weather11[1],
weather2 );
wts.add(wt1);
imgPic=(ImageView) view.findViewById(R.id.img_wt_m_pic);
txtSX=(TextView) view.findViewById(R.id.txt_sx);
txtCloud=(TextView) view.findViewById(R.id.txt_wt_cloud);
txtTemp=(TextView) view.findViewById(R.id.txt_wt_temp);
txtInfo=(TextView) view.findViewById(R.id.txt_wt_advic);
int value11 = cls.getDeclaredField("a_" + weather44[0]+"_d")
.getInt(null);
imgPic.setImageResource(value11);
txtSX.setText(mApp.post);
txtCloud.setText(weather2);
txtTemp.setText(weather1);
txtInfo.setText(weather.get(10));
String weather5 = weather.get(12);
String weather6 = weather.get(13);
String weather7 = weather.get(14);
String weather8 = weather.get(15);
String[] weather66 = weather6.split(" ");
String[] weather88 = weather8.split("\\.");
int value2 = cls.getDeclaredField("a_" + weather88[0])
.getInt(null);
WTEntity wt2 = new WTEntity(value2, weather66[0],weather66[1],
weather5 );
wts.add(wt2);
String weather9 = weather.get(17);
String weather10 = weather.get(18);
String weather111 = weather.get(19);
String weather112 = weather.get(20);
String[] weather100 = weather10.split(" ");
String[] weather1122 = weather112.split("\\.");
int value3 = cls.getDeclaredField("a_" + weather1122[0])
.getInt(null);
WTEntity wt3 = new WTEntity(value3, weather100[0],weather100[1],
weather9 );
wts.add(wt3);
mApp.cachewts=wts;
mApp.wtpicId=value11;
mApp.wtSx=mApp.post;
mApp.wtcloud=weather2;
mApp.wttemp=weather1;
mApp.wtinfo=weather.get(10);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
UIHelper.hideDialogForLoading();
Toast.makeText(getActivity(), "数据加载失败,请稍后再试",
Toast.LENGTH_LONG).show();
}
}
});
}
还有一点大家要是想让自己app显示自定义图片,下载中国天气网上的图片包,将自己的图片名称改为对应天气图标即可
- 访问中国天气网获取最近三天的数据
- 获取中国天气网天气预报json数据的api接口
- 用Pyhton获取中国天气网数据
- Json获取中国天气网天气的代码
- android从中国天气网获取天气
- Qt5获取并解析中国天气网数据
- Python 抓取中国天气网天气数据
- Android使用"中国天气网"API数据通过城市名称获取天气情况
- Android使用"中国天气网"API数据通过城市名称获取天气情况
- 中国天气网城市数据
- 利用google天气api获取最近的天气
- weather中国气象网的天气城市编码获取地址
- 中国天气网的中央气象台实时数据接口
- 中国天气网的中央气象台实时数据接口
- 中国天气网的中央气象台实时数据接口
- 中国天气网的中央气象台实时数据接口
- 中国天气网的中央气象台实时数据接口之管中窥豹
- Android解析中国天气网的Json数据
- 素数筛选和合数分解
- 通过Servlet理解MVC框架的基本实现原理
- linux查看日志命令
- RecyclerView的使用
- activity切换动画全局设定
- 访问中国天气网获取最近三天的数据
- 有序链表的归并
- 同时运行两个TOMCAT的配置
- Windows平台下一些好玩的技巧
- Swift学习之六:元组(Tuples)
- transient
- DAPM 概述(中文翻译)/ dapm.txt
- 如何开启ubuntu共享桌面
- bzoj1042[HAOI2008]硬币购物