[整理]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 这篇博客一致. 下面我们将重点详细解 ...
随机推荐
- MemberwiseClone和DeepClone
文章转自:http://www.cnblogs.com/zhangji/archive/2011/02/23/1961897.html MemberwiseClone 方法创建一个浅表副本,具体来说就 ...
- System.BadImageFormatException: 未能加载文件或程序集""或它的某一个依赖项。试图加载格式不正确的程序。
解决方法: 1.更改程序集的生成目标平台为[Any CPU],或者针对平台进行编译. 项目右键->[属性]->[生成]->[生成目标平台] 2.尝试一下修改线程池设置为32位支持.
- iptables常规使用
0x00 简介 iptables防火墙由Netfilter项目开发,自linux2.4就融入了内核.linux内核中的Netfilter框架可将数据包操作函数挂接至网络栈.iptables便在这个框架 ...
- python时间模块-time和datetime
时间模块 python 中时间表示方法有:时间戳,即从1975年1月1日00:00:00到现在的秒数:格式化后的时间字符串:时间struct_time 元组. struct_time元组中元素主要包括 ...
- 用DOS命令配置服务开机自启动
2016-08-19 15:01 Create 使用命令 sc config 参考博客:http://blog.csdn.net/it1988888/article/details/7992626 ...
- 在Nginx中部署基于IP的虚拟主机
一.虚拟主机概念 虚拟主机是在网络服务器上划分出一定的磁盘空间供用户放置站点.应用组件等,提供必要的站点功能.数据存放和传输功能.所谓虚拟主机,也叫"网站空间", 就是把一台运行在 ...
- https 页面中引入 http 资源的解决方式
相对协议 应用场景 浏览器默认是不允许在 https 里面引用 http 资源的,一般都会弹出提示框. 用户确认后才会继续加载,用户体验非常差. 而且如果在一个 https 页面里动态的引入 http ...
- css009 装饰网站的导航
css009 装饰网站的导航 1. 选择定义样式的链接 1.连接的状态: A.未访问 a:link{C;} B.已访问 a:visited{ color : red; } C.鼠标 ...
- 《零成本实现Web性能测试:基于Apache JMeter》读书笔记
1.性能测试概念 性能测试目的: 评估系统能力,验证系统是否符合预期性能指标 识别系统中的弱点 系统调优,改进系统性能 检测长时间运行可能发生的问题,揭示隐含问题 验证稳定性.可靠性 常见性能指标 B ...
- phpcms后台获取当前登录账号的数据
$amdinid=$_SESSION['userid'];$infoadmin=$this->admin->get_one(array('userid'=>$amdinid)); v ...