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的更多相关文章

  1. 基于Android开发的天气预报app(源码下载)

    原文:基于Android开发的天气预报app(源码下载) 基于AndroidStudio环境开发的天气app -系统总体介绍:本天气app使用AndroidStudio这个IDE工具在Windows1 ...

  2. 收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范,免费的设计素材等。

    AndroidDevTools Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.An ...

  3. Android开发--二维码开发应用(转载!)

    android项目开发 二维码扫描   基于android平台的二维码扫描项目,可以查看结果并且链接网址 工具/原料 zxing eclipse 方法/步骤   首先需要用到google提供的zxin ...

  4. Android开发二维码之坑

    之前一直做的是.NET开发用的是C#语言,近段时间由于做一个APP这才用上了java,在二维码扫描整合到APP里面遇到扫描二维码之后没有返回值,经过反复的尝试最后终于拿到了返回值,之后觉得很有必要记录 ...

  5. (转)Android开发出来的APP在手机的安装路径是?

    一.安装路径在哪? Android应用安装涉及到如下几个目录: system/app系统自带的应用程序,无法删除.data/app用户程序安装的目录,有删除权限.安装时把apk文件复制到此目录.dat ...

  6. [整理]Android开发(一)环境安装

    所有相关下载均可通过http://www.androiddevtools.cn/下载 安装JAVA JDK 下载Windows x64 180.44 MB jdk-8u45-windows-x64.e ...

  7. Android开发之创建App Widget和更新Widget内容

    App WidgetsApp Widgets are miniature application views that can be embedded in other applications (s ...

  8. Android开发之定义app在手机的安装位置

    定义app在手机的安装位置,可以通过在清单文件中添加属性 android:installLocation="" 该属性有三个值:auto(自动),preferExternal(外部 ...

  9. android 开发 实现一个app的引导页面,使用ViewPager组件(此引导的最后一页的Button会直接写在最后一页布局里,跟随布局滑进滑出)

    基本ViewPager组件使用方式与我之前写的https://blog.csdn.net/qq_37217804/article/details/80332634 这篇博客一致. 下面我们将重点详细解 ...

随机推荐

  1. GDUT校赛

    题目链接:http://4.gdutcode.sinaapp.com/contest.php?cid=1021 F 题意:给出n和m,要求满足gcd(x,y)=n && lcm(x,y ...

  2. CSS基础知识真难啊

    CSS层叠样式表Cascading Style Sheets CSS派生选择器(上下文选择器): 后代选择器:h1  strong {color:red;}第一个参数和第二个参数之间的代数是可以无限的 ...

  3. ASP.NET MVC4 执行流程

    MVC在底层和传统的asp.net是一致的,在底层之上,相关流程如下: 1)Global.asax里,MvcApplication对象的Application_Start()事件中,调用 RouteC ...

  4. CF 321B Kefa and Company(贪心)

    题目链接: 传送门 Kefa and Company time limit per test:2 second     memory limit per test:256 megabytes Desc ...

  5. PHP之:多图上传

    撰写日期:2016-6-30 15:17:35 Thursday 参考 http://a3147972.blog.51cto.com/2366547/1381136 (08-05ThinkPHP+sw ...

  6. plist文件的使用

    什么是plist文件 直接将数据写在代码里面,不是一种合理的做法.如果经常改,就要经常翻开对应的代码进行修改,造成代码扩展性低 因此,可以考虑将经常变的数据放在文件中进行存储,程序启动后从文件中读取最 ...

  7. hibernate基础增删查改简单实例

    hibernate 基础理论知识网上很多,可以百度和google.这里不做多的介绍,以一个User表来开展例子 建一个web-project 我这里用了junit单元测试环境来进行增删查改的测试,别的 ...

  8. 《JavaScript权威指南》学习笔记 第三天 找个对象

    现实生活中真的对象没有找到,在JavaScript 里左一个对象又一个对象,搞的我也是晕晕乎乎不知所云.人事复杂,人心难懂.我虽然是文科生,但是也不善于巧言.还是在js里面找找对象吧.那么我们今天就从 ...

  9. codeforce B Island Puzzle

    B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  10. NSXMLParser解析本地.xml数据(由于like7xiaoben写的太好了,我从她那里粘贴过来的)

    NSXMLParser解析简要说明 .是sax方法解析 .需要创建NSXMLParser实例 (alloc) 并创建解析器 (initWithData:) 为解析器定义委托 (setDelegate: ...