[整理]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 这篇博客一致. 下面我们将重点详细解 ...
随机推荐
- [NOIP2014] 提高组 洛谷P2296 寻找道路
题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...
- Akka: actor应用的一些小结
1.消息: 1) case class是scala中一个不可变对象(当然你可以让他成为可变的),通过不可变对象来进行消息传递可以更加明确内容,也能保证线程安全 2) 在Java中如果你将class对象 ...
- SQL请求优化——请求次数统计,SQL写操作稀释
引言 前几天做了这么一个东西:一个游戏中有个活动页面,活动页面有个商品,商品下面要显示该商品实浏览次数,就相当于是用户每出发一次请求这个浏览总次数都会添加一次,这个问题很简单,每次浏览的时候去数据库中 ...
- django redis VS memcache 区别简介
https://www.v2ex.com/t/142644 http://stackoverflow.com/questions/10558465/memcached-vs-redis 简单来说: r ...
- iOS - 利用runtime加深对基础知识的理解
利用runtime加深对基础知识的理解 如果对runtime需要学习,可以看这篇,以下仅作为学习笔记,相互交流. runtime的头文件: #import <objc/runtime.h> ...
- BZOJ1915: [Usaco2010 Open]奶牛的跳格子游戏
权限题,没有传送门. 这很显然是一道DP题,刚看完题目可能会比较懵逼.这道题如果不要求回去,那么就是一道很裸的DP题.但是本题要求回去而且回去的格子的前一个格必须是之前经过的. 先不考虑回去的路程,对 ...
- 深入JVM-性能监控工具
一.Linux下的性能监控工具 1.1 显式系统整体资源使用情况-top命令 top命令的输出可以分为两个部分:前半部分是系统统计信息,后半部分是进程信息. 在统计信息中,第一行是任务队列信息,他的结 ...
- 从Paxos到ZooKeeper-四、ZooKeeper技术内幕
本文将从系统模型.序列化与协议.客户端工作原理.会话.服务端工作原理以及数据存储等方面来揭示ZooKeeper的技术内幕. 一.系统模型 1.1 数据模型 ZooKeeper的视图结构使用了其特有的& ...
- SVN Access to ‘/svn/Test/!svn/me’ forbidden,不能更新解决办法
今天上班,使用公司配置的电脑进行项目的更新.SVN报如下错误, SVN Access to '/svn/Test/!svn/me' forbidden,不能更新解决办法 很有意思: 开始以为自己的SV ...
- 点亮第一个LED灯
1.代码: #include <reg52.h> //<reg51.h> 包含52单片机寄存器库sbit led = P1^0; //只有地址可以被8整除的 才可以用s ...