简单的天气预报app
来源:互联网 发布:手机版 淘宝 人工客服 编辑:程序博客网 时间:2024/05/16 08:36
简约便捷的音天气预报app
自己最近慢慢摸索写了一个简约便捷的天气预报app,先发上来供大家参考和自己复习学习。## api ##首先我们需要一个api,我采用的是百度天气的api,具体步骤大家可以参考http://lbsyun.baidu.com/index.php?title=car/api/weather
xml布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/bj2" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:layout_width="wrap_content" android:layout_height="80dp" android:orientation="horizontal"> <ImageView android:id="@+id/imgtop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/duoyun" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" /> <TextView android:id="@+id/wendu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="50dp" android:layout_marginTop="20dp" android:textColor="@color/colorAccent" /> <EditText android:id="@+id/etcityname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#ffffff" android:hint="输入城市名称" android:textColorHint="#ffffff" android:textSize="24dp"/> <Button android:id="@+id/sousuo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dp" android:textColor="#ffffff" android:text="搜索"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:gravity="center"> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center"> <TextView android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="80dp" android:textColor="#ffffff"/> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#ffffff"/><ScrollView android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="5dp" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="wind" android:gravity="center" android:textColor="#ffffff" android:textSize="20dp"/> <TextView android:id="@+id/wind" android:layout_width="match_parent" android:layout_height="60dp" android:textSize="20dp" android:textColor="#ffffff" android:gravity="center"/> </LinearLayout> <TextView android:layout_width="1dp" android:layout_height="match_parent" android:background="#55000000" /> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="weather" android:gravity="center" android:textColor="#ffffff" android:textSize="20dp"/> <TextView android:id="@+id/temperature" android:layout_width="match_parent" android:layout_height="60dp" android:textSize="20dp" android:textColor="#ffffff" android:gravity="center"/> </LinearLayout> <TextView android:layout_width="1dp" android:layout_height="match_parent" android:background="#55000000" /> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="PM2.5" android:gravity="center" android:textColor="#ffffff" android:textSize="20dp"/> <TextView android:id="@+id/pm25" android:layout_width="match_parent" android:layout_height="60dp" android:textSize="20dp" android:textColor="#ffffff" android:gravity="center"/> </LinearLayout> <TextView android:layout_width="1dp" android:layout_height="match_parent" android:background="#55000000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#ffffff"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="指数" android:textSize="40dp" android:textColor="@color/colorAccent"/> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#ffffff"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:textColor="#ffffff" android:gravity="center" android:textSize="24dp" android:text="穿衣"/> <TextView android:id="@+id/chuanyi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="20dp" android:layout_marginLeft="10dp" android:text="适合穿衣"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:textColor="#ffffff" android:gravity="center" android:textSize="24dp" android:text="洗车"/> <TextView android:id="@+id/xiche" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="20dp" android:layout_marginLeft="10dp" android:text="适合穿衣"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="24dp" android:gravity="center" android:text="运动"/> <TextView android:id="@+id/ganmao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="20dp" android:layout_marginLeft="10dp" android:text="适合穿衣"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="24dp" android:gravity="center" android:text="感冒"/> <TextView android:id="@+id/yundong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="20dp" android:layout_marginLeft="10dp" android:text="适合穿衣"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:textColor="#ffffff" android:gravity="center" android:textSize="24dp" android:text="紫外线"/> <TextView android:id="@+id/ziwaixian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="20dp" android:layout_marginLeft="10dp" android:text="适合穿衣"/> </LinearLayout> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#ffffff"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_weight="1" android:orientation="vertical" android:layout_width="0dp" android:layout_height="wrap_content"> <TextView android:layout_gravity="center" android:id="@+id/diertian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dp" android:textColor="#ffffff"/> <ImageView android:layout_gravity="center" android:id="@+id/imdiertian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/duoyun"/> <TextView android:layout_gravity="center" android:id="@+id/wdiertian" android:layout_width="wrap_content" android:layout_height="45dp" android:textColor="#ffffff" android:textSize="20dp"/> <TextView android:layout_gravity="center" android:id="@+id/tdiertian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="20dp"/> </LinearLayout> <LinearLayout android:layout_weight="1" android:orientation="vertical" android:layout_width="0dp" android:layout_height="wrap_content"> <TextView android:id="@+id/disantian" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dp" android:textColor="#ffffff"/> <ImageView android:id="@+id/imdisantian" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/duoyun"/> <TextView android:layout_gravity="center" android:id="@+id/wdisantian" android:layout_width="wrap_content" android:layout_height="45dp" android:textColor="#ffffff" android:textSize="20dp"/> <TextView android:id="@+id/tdisantian" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="20dp"/> </LinearLayout> <LinearLayout android:layout_weight="1" android:orientation="vertical" android:layout_width="0dp" android:layout_height="wrap_content"> <TextView android:id="@+id/disitian" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dp" android:textColor="#ffffff"/> <ImageView android:id="@+id/imdisitian" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/duoyun"/> <TextView android:layout_gravity="center" android:id="@+id/wdisitian" android:layout_width="wrap_content" android:layout_height="45dp" android:textColor="#ffffff" android:textSize="20dp"/> <TextView android:id="@+id/tdisitian" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="20dp"/> </LinearLayout> </LinearLayout></LinearLayout></ScrollView></LinearLayout>
主要是LinearLayout和一个ScrollView的嵌套很简单就不多说了。
网络请求
/20:40:90:AE:E8:19:FD:95:21:F2:CD:D5:72:D3:EE:CB:38:F9:B7:9C这是发布版SHA1,com.example.administrator.zhuanjia这是包名/
private void sendRequestWithHttpURLConnection() { // 开启线程来发起网络请求
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
StringBuilder cityname=new StringBuilder();
try {
cityname.append(“http://api.map.baidu.com/telematics/v3/weather?location=“).
append(URLEncoder.encode(etcityname.getText().toString(),”UTF-8”)).
append(“&mcode=20:40:90:AE:E8:19:FD:95:21:F2:CD:D5:72:D3:EE:CB:38:F9:B7:9C;com.example.administrator.zhuanjia&output=json&ak=P0SE4gI053GYaVRONbWwR8Bz13a5OwiC”);
URL url = new URL(cityname.toString());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(“GET”);
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream(); // 下面对获取到的输入流进行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
Message message = new Message();
message.what = SHOW_RESPONSE;
// 将服务器返回的结果存放到Message中
message.obj = response.toString();
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
} finally {
//if (connection != null) {
// connection.disconnect();
//}
}
}
}).start();
}
对返回的JSON进行解析
public List<Map<String, Object>> getInformation(String jonString) throws Exception { JSONObject dataOfJson =new JSONObject(jonString); JSONArray results=dataOfJson.getJSONArray("results"); JSONObject results0=results.getJSONObject(0); JSONArray index = results0.getJSONArray("index"); JSONObject index0 = index.getJSONObject(0);//穿衣 JSONObject index1 = index.getJSONObject(1);//洗车 JSONObject index2 = index.getJSONObject(2);//感冒 JSONObject index3 = index.getJSONObject(3);//运动 JSONObject index4 = index.getJSONObject(4);//紫外线强度 JSONArray weather_data = results0.getJSONArray("weather_data");//weather_data中有四项 JSONObject weather_data0 = weather_data.getJSONObject(0);//今天 JSONObject weather_data1 = weather_data.getJSONObject(1);//明天 JSONObject weather_data2 = weather_data.getJSONObject(2);//后天 JSONObject weather_data3 = weather_data.getJSONObject(3);//大后天 List<Map<String, Object>> all = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("location",results0.getString("currentCity")); map.put("pm25",results0.getString("pm25")); map.put("chuanyi",index0.getString("des")); map.put("xiche",index1.getString("des")); map.put("ganmao",index2.getString("des")); map.put("yundong",index3.getString("des")); map.put("ziwaixian",index4.getString("des")); map.put("date",weather_data0.getString("date")); map.put("weather",weather_data0.getString("weather")); map.put("wind",weather_data0.getString("wind")); map.put("temperature",weather_data0.getString("temperature")); map.put("date1",weather_data1.getString("date")); map.put("weather1",weather_data1.getString("weather")); map.put("temperature1",weather_data1.getString("temperature")); map.put("date2",weather_data2.getString("date")); map.put("weather2",weather_data2.getString("weather")); map.put("temperature2",weather_data2.getString("temperature")); map.put("date3",weather_data3.getString("date")); map.put("weather3",weather_data3.getString("weather")); map.put("temperature3",weather_data3.getString("temperature")); all.add(map); return all; }
将获取的数据更新在UI上
private Handler handler=new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case SHOW_RESPONSE: String response = (String) msg.obj; // 在这里进行UI操作,将结果显示到界面上 Util util = new Util(); try { List<Map<String, Object>> all = util .getInformation(response); Iterator<Map<String, Object>> iterator = all.iterator(); while (iterator.hasNext()) { Map<String, Object> map = iterator.next(); //Log.d("天气", map.get("weather").toString()); city.setText(map.get("location") .toString()); backgrand(imgtop,map.get("weather") .toString()); pm25.setText(map.get("pm25").toString()); chuanyi.setText(map.get("chuanyi").toString()); xiche.setText(map.get("xiche").toString()); ganmao.setText(map.get("ganmao").toString()); yundong.setText(map.get("yundong").toString()); ziwaixian.setText(map.get("ziwaixian").toString()); wind.setText(map.get("wind").toString()); temperature.setText(map.get("temperature").toString()); date.setText(map.get("date").toString()); diertian.setText(map.get("date1").toString()); tdiertian.setText(map.get("temperature1").toString()); disantian.setText(map.get("date2").toString()); tdisantian.setText(map.get("temperature2").toString()); disitian.setText(map.get("date3").toString()); tdisitian.setText(map.get("temperature3").toString()); wdiertian.setText(map.get("weather1").toString()); wdisantian.setText(map.get("weather2").toString()); wdisitian.setText(map.get("weather3").toString()); backgrand(imdiertian,map.get("weather1") .toString()); backgrand(imdisantian,map.get("weather2") .toString()); backgrand(imdisitian,map.get("weather3") .toString()); } } catch (Exception e) { e.printStackTrace(); } } } };
上面就是我的主要代码,本人菜鸟一枚如有错误或者不妥请大家指正,我的源码在我的github上欢迎大家关注我https://github.com/zkandroid/zhuanjia
- 简单的天气预报app
- Android上实现一个简单的天气预报APP(十五) 发布天气预报APP
- 天气预报APP简单点的,准备做求大神出招
- Android上实现一个简单的天气预报APP(一) 设计
- Android上实现一个简单的天气预报APP(十二) 未来三天的天气预报
- 简单的android天气预报
- 简单的天气预报 (一)
- 简单的天气预报 (二)
- 简单的天气预报 (三)
- 简单的天气预报 (四)
- 简单的天气预报 (五)
- 简单的天气预报 (六)
- Android简单的天气预报
- 一个简单的天气预报
- Android上实现一个简单的天气预报APP(十一) Preference存储最近一次查询的城市
- App天气预报
- Android上实现一个简单的天气预报APP(二) 配置布局
- Android上实现一个简单的天气预报APP(三) 获取网络数据
- POJ 3682 (dp)
- Amdroid中微信支付遇到的问题
- 通信录
- 八皇后GO语言实现
- linux命令行参数解析函数 getopt
- 简单的天气预报app
- 可编辑DIV,输入图片
- Android横屏竖屏切换的问题
- 解决plsql连接oracle服务器(云服务器)自动断开问题
- 记第一次知网爬虫实践
- Spring AOP介绍
- 使用bootstrapvalidator的remote验证经验
- Android Loading SQLite Data Into SpinnerView
- Shell部分2