[整理]Android开发(二)-Weather App
private class WeatherData{
private String _weatherDescription;
private Integer _currentTemperature;
private Integer _LowTemperature;
private Integer _highTemperature;
public WeatherData(String weatherDescription,Integer currentTemperature, Integer LowTemperature, Integer highTemperature){
_weatherDescription = weatherDescription;
_currentTemperature = currentTemperature;
_LowTemperature = LowTemperature;
_highTemperature = highTemperature;
}
public String getWeatherDescription(){
return _weatherDescription;
}
public Integer getCurrentTemperature(){
return _currentTemperature;
}
public Integer getLowTemperature(){
return _LowTemperature;
}
public Integer getHighTemperature(){
return _highTemperature;
}
}
//异步获取天气数据,并更新主UI线程
private class HttpRequestAsyncTask extends AsyncTask{
private WeatherData loadWeatherData(String requestUrl){
WeatherData weatherData = null;
String dataResponse = null;
//通过API,获取天气数据
try{
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//conn.setConnectTimeout(10000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("apikey","XXX");
conn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
StringBuffer buffer = new StringBuffer();
String lineBuffer = null;
while((lineBuffer=reader.readLine()) != null){
buffer.append(lineBuffer);
}
reader.close();
conn.disconnect();
dataResponse = buffer.toString();
}
catch (Exception ex){
ex.printStackTrace();
System.out.println("error->"+ex.getMessage());
}
//解析对返回的json数据
/** api return value
* {
"errNum": 0,
"errMsg": "success",
"retData": {
"city": "上海",
"pinyin": "shanghai",
"citycode": "101020100",
"date": "15-06-03",
"time": "11:00",
"postCode": "200000",
"longitude": 121.445,
"latitude": 31.213,
"altitude": "19",
"weather": "阴",
"temp": "25",
"l_tmp": "20",
"h_tmp": "25",
"WD": "东北风",
"WS": "微风(<10m/h)",
"sunrise": "04:50",
"sunset": "18:54"
}
}
* */
try{
JSONTokener jsonParser = new JSONTokener(dataResponse);
JSONObject weatherInfo = (JSONObject)jsonParser.nextValue();
int returnValue = weatherInfo.getInt("errNum");
if(returnValue != 0){
System.out.println( "api get error->" + returnValue);
}
else{
JSONObject jsonData = weatherInfo.getJSONObject("retData");
weatherData = new WeatherData(jsonData.getString("weather"),jsonData.getInt("temp"),jsonData.getInt("l_tmp"),jsonData.getInt("h_tmp"));
}
}
catch (JSONException ex){
ex.printStackTrace();
System.out.println( "api get error->" + ex.getMessage());
}
return weatherData;
}
@Override
protected Object doInBackground(Object[] params) {
String requestUrl = params[0].toString();
return this.loadWeatherData(requestUrl);
}
@Override
protected void onPostExecute(Object o) {
if(o==null){
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
alertBuilder.setTitle(R.string.alert_title_load_weather_failed)
.setMessage(R.string.alert_message_load_weather_failed);
alertBuilder.create();
}
else{
//super.onPostExecute(o);
WeatherData weatherData = (WeatherData)o;
//更新UI
CurrentTempratureTextView.setText(weatherData.getCurrentTemperature()+"℃");
}
}
}
[整理]Android开发(二)-Weather App的更多相关文章
- 基于Android开发的天气预报app(源码下载)
原文:基于Android开发的天气预报app(源码下载) 基于AndroidStudio环境开发的天气app -系统总体介绍:本天气app使用AndroidStudio这个IDE工具在Windows1 ...
- 收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范,免费的设计素材等。
AndroidDevTools Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.An ...
- Android开发--二维码开发应用(转载!)
android项目开发 二维码扫描 基于android平台的二维码扫描项目,可以查看结果并且链接网址 工具/原料 zxing eclipse 方法/步骤 首先需要用到google提供的zxin ...
- Android开发二维码之坑
之前一直做的是.NET开发用的是C#语言,近段时间由于做一个APP这才用上了java,在二维码扫描整合到APP里面遇到扫描二维码之后没有返回值,经过反复的尝试最后终于拿到了返回值,之后觉得很有必要记录 ...
- (转)Android开发出来的APP在手机的安装路径是?
一.安装路径在哪? Android应用安装涉及到如下几个目录: system/app系统自带的应用程序,无法删除.data/app用户程序安装的目录,有删除权限.安装时把apk文件复制到此目录.dat ...
- [整理]Android开发(一)环境安装
所有相关下载均可通过http://www.androiddevtools.cn/下载 安装JAVA JDK 下载Windows x64 180.44 MB jdk-8u45-windows-x64.e ...
- Android开发之创建App Widget和更新Widget内容
App WidgetsApp Widgets are miniature application views that can be embedded in other applications (s ...
- Android开发之定义app在手机的安装位置
定义app在手机的安装位置,可以通过在清单文件中添加属性 android:installLocation="" 该属性有三个值:auto(自动),preferExternal(外部 ...
- android 开发 实现一个app的引导页面,使用ViewPager组件(此引导的最后一页的Button会直接写在最后一页布局里,跟随布局滑进滑出)
基本ViewPager组件使用方式与我之前写的https://blog.csdn.net/qq_37217804/article/details/80332634 这篇博客一致. 下面我们将重点详细解 ...
随机推荐
- Restful api介绍
网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...
- ueditor的优酷插件模式开发,目前开发了腾讯视频转换插件
项目相关地址 源码:https://github.com/easonjim/ueditor_plugin bug提交:https://github.com/easonjim/ueditor_plugi ...
- hdu3932 模拟退火
模拟退火绝对是从OI--ACM以来接触过的所有算法里面最黑科技的orz 题意:地上有一堆hole,要找一个点,使得(距离该点最远的hole的距离)最小. sol:本来想套昨天的模拟退火模板,初值(0, ...
- HDU 2795 Billboard
Description 在学校的入口处有一个巨大的矩形广告牌,高为h,宽为w.所有种类的广告都可以贴,比如ACM的广告啊,还有餐厅新出了哪些好吃的,等等.. 在9月1号这天,广告牌是空的,之后广告 ...
- DayuCMS 1.525 /include/global.func.php Foreground Arbitrary Code Execution
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Relevant Link: http://joychou.org/in ...
- 在浏览器中直接生成 PDF
well, 如果有人在准备开发一个浏览器端的 PDFCreator,那么,你需要知道的是,已经有人在这么做了.不过,他的做法显然更靠谱一些——先开发一个可以将基本元素放入 PDF 的库.太简单?no! ...
- PHP设计模式(二)
从最近开始我给自己定了个目标,每周至少更新2篇博客,用来记录自己在上一周里面遇到的问题或者想出的新点子,一方面对自己掌握的知识进行记录,免得时间久了忘得一干二净,二来我的博文虽然不怎么好但也许会对一小 ...
- 个人作业—Week2:微软必应词典案例分析
调研.评测 bug报告: 标题:Window 10版必应词典客户端口语练习功能无法使用 环境:Window 10, 微软必应词典(UWP) 版本2.6.1.0,屏幕无重力感应模块 重现步骤: 1) ...
- jQuery基础(2)-- jQuery 选择器
jQuery 选择器允许您对 HTML 元素组或单个元素进行操作.jQuery 选择器基于元素的 id.类.类型.属性.属性值等"查找"(或选择)HTML 元素. 它基于已经存在的 ...
- js022-高级技巧
js022-高级技巧 本章内容: 使用高级函数 防篡改对象 Yielding Timers 22.1 高级函数 1.安全的类型检测 2.作用域安全的构造函数 构造函数实际上是一个使用new操作符调用的 ...