Android-查询天气
2016-06-04 13:52
465 查看
目标效果:
点击按钮,即可返回数据,在TextView和logcat中显示获取到的天气数据。
1.首先在聚合数据中注册账号:https://www.juhe.cn/login
2.进入我的数据后,点击申请数据,免费为500次。
3.申请后会在我的数据显示当前数据的信息,包括Appkey和剩余次数。
4.点击测试。
5.输入想要查询的城市名称,点击发送请求。
6.请求后会在请求详情里显示网址以及下方显示获取到的数据,网址格式为“http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=您申请的KEY”,其中%E8%8B%8F%E5%B7%9E部分为城市名称,该网址示例为苏州地区的天气,当在浏览器中可直接输入中文,但是程序中不可以,需要将中文转换为字符编码在进行访问。
7.开始编写代码,activity_main.xml页面放置需要的Button和TextView控件。
activity_main.xml页面:
8.MainActivity.java页面进行数据的获取和显示。
MainActivity.java页面:
9.添加权限
9.运行程序就可以显示获取到的天气数据了。
点击按钮,即可返回数据,在TextView和logcat中显示获取到的天气数据。
1.首先在聚合数据中注册账号:https://www.juhe.cn/login
2.进入我的数据后,点击申请数据,免费为500次。
3.申请后会在我的数据显示当前数据的信息,包括Appkey和剩余次数。
4.点击测试。
5.输入想要查询的城市名称,点击发送请求。
6.请求后会在请求详情里显示网址以及下方显示获取到的数据,网址格式为“http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=您申请的KEY”,其中%E8%8B%8F%E5%B7%9E部分为城市名称,该网址示例为苏州地区的天气,当在浏览器中可直接输入中文,但是程序中不可以,需要将中文转换为字符编码在进行访问。
7.开始编写代码,activity_main.xml页面放置需要的Button和TextView控件。
activity_main.xml页面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/send_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Request" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/response" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
8.MainActivity.java页面进行数据的获取和显示。
MainActivity.java页面:
package com.example.networktest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button sendRequest; private TextView responseText; public static final int SHOW_RESPONSE = 0; private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case SHOW_RESPONSE: String response = (String) msg.obj; // 在这里进行UI操作,将结果显示到界面上 responseText.setText(response); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendRequest = (Button) findViewById(R.id.send_request); responseText = (TextView) findViewById(R.id.response); sendRequest.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { /*获取天气数据*/ sendRequestWithHttpURLConnection(); } }); } /** * 获取天气数据 */ protected void sendRequestWithHttpURLConnection() { new Thread() { //创建线程 @Override public void run() { URL url; HttpURLConnection connection = null; try { String cityName = URLEncoder.encode("滨州", "utf-8"); //将汉字转换为编码 url = new URL( "http://v.juhe.cn/weather/index?format=2&cityname="+cityName+"&key=你自己的key"); Log.i("MainActivity","url"+url);//设置访问地址 connection = (HttpURLConnection) url.openConnection(); //实例化connection对象 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); } Log.i("MainActivity","response=" + response.toString()); parseWeatherWithJSON(response.toString()); Message message = new Message(); message.what = SHOW_RESPONSE; // 将服务器返回的结果存放到Message中 message.obj = response.toString(); handler.sendMessage(message); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } } }.start(); } protected void parseWeatherWithJSON(String response) { try { JSONObject jsonObject=new JSONObject(response); String resultcode=jsonObject.getString("resultcode"); if(resultcode.equals("200")){ JSONObject resultObject=jsonObject.getJSONObject("result"); JSONObject todayObject=resultObject.getJSONObject("today"); String date_y=todayObject.getString("date_y"); String week=todayObject.getString("week"); String temperature=todayObject.getString("temperature"); Log.d("MainActivity", "date_y="+date_y+"week="+week+"temp="+temperature); } } catch (JSONException e) { e.printStackTrace(); } } protected void parseWithJSON(String response) { try { JSONArray jsonArray = new JSONArray(response); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String id = jsonObject.getString("id"); String name = jsonObject.getString("name"); String version = jsonObject.getString("version"); Log.d("MainActivity", "id=" + id + "name=" + name + "version=" + version); } } catch (JSONException e) { e.printStackTrace(); } } }
9.添加权限
<uses-permission android:name="android.permission.INTERNET"/>
9.运行程序就可以显示获取到的天气数据了。
相关文章推荐
- Java/Android引用类型及其使用分析
- android控件-ExpandableListView
- Android 获得对一个设备节点的访问权限
- 【android】监听编辑EditText时点击软键盘事件 + 隐藏软键盘 + keycode汇总
- <Android 基础
- android Fragment相关概念简介
- android.text.format.Formatter计算文件大小超方便的工具类
- Android json详解
- android中listview的item可以获取到点击事件的方法
- Android 开发那些走不完的配置套路一
- Android自定义控件之滑动开关
- <Android 基础(四)> RecyclerView
- <Android 基础(四)> RecyclerView
- Android SDK无法更新或下载很慢
- Android中Activity四种启动模式和taskAffinity属性详解
- android蓝牙开发 蓝牙设备的查找和连接
- android蓝牙主动发起配对实例
- android开发之蓝牙配对连接的方法
- Android研究院之应用开发线程池的经典使用(二十九)
- Android内存优化大全(中)