Android解析聚合数据之天气预报
免费天气预报API:https://www.juhe.cn/docs/api/id/73 ,申请APPKEY
MainActivity.java
<span style="font-size:14px;">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 {
// url = new
// URL("http://10.2.5.119:8080/Server/getData.json");
String cityName = URLEncoder.encode("滨州", "utf-8");
url = new URL(
"http://v.juhe.cn/weather/index?format=2&cityname="
+ cityName
+ "&key=ab9d7e2007472d723baf71fcdc4ba094");
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);
}
System.out.println("response=" + response.toString());
//parseWithJSON(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();
}
} }</span>
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>
Android解析聚合数据之天气预报的更多相关文章
- 聚合数据全国天气预报api接口
查询天气预报在APP中常用的一个常用功能,聚合数据全国天气预报api接口可以根据根据城市名/id查询天气.根据IP查询天气.据GPS坐标查询天气.查询城市天气三小时预报,并且支持全国不同城市天气预报查 ...
- Android通过聚合数据API实现天气预报
使用聚合数据的API 聚合数据地址:https://www.juhe.cn/ 在数据服务->生活常用->全国天气预报,申请天气预报的API使用的KEY 保存请求示例的地址,把您申请的KEY ...
- 聚合数据全国天气预报API--ajax 通过城市名取数据
聚合数据天气预报API:https://www.juhe.cn/docs/api/id/39 接口地址:http://v.juhe.cn/weather/index 支持格式:json/xml 请求方 ...
- Android解析Json数据之Gson解析
Gson是谷歌官方提供的解析json数据的工具类.json数据的解析能够使用JSONObject和JSONArray配合使用解析数据,可是这样的原始的方法对于小数据的解析还是有作用的,可是陪到了复杂数 ...
- android 解析服务器数据使用json还是xml方式
整理自百度搜索: 现在的Android应用程序,几乎没有不与服务端交换数据的了!那么,android应用在与服务端交换数据的时候,我们有哪些选择呢?哪种数据交换格式要更好吗?下面文章简单为 andro ...
- Android解析json数据
Json数据 [{"code":"110000","sheng":"11","di":"0 ...
- [转]Android解析json数据
1.json格式 2.json解析 3.gson解析 4.fastjson解析 一.Json格式 json一种轻量级的数据交换格式.在网络上传输交换数据一般用xml, json. 两种结构: 1)对象 ...
- Android解析中国天气接口JSon数据,应用于天气查询!
android解析Json数据是比较常见的一种操作.也是客户端和服务器进行数据交互的桥梁.下面就来看一看在android中解析JSon数据的方法吧. 首先要想获得Json数据,就必须访问相关的网络接口 ...
- android基础---->JSON数据的解析
上篇博客,我们谈到了XML两种常用的解析技术,详细可以参见我的博客(android基础---->XMl数据的解析).网络传输另外一种数据格式JSON就是我们今天要讲的,它是比XML体积更小的数据 ...
随机推荐
- 只显示前几条数据的sql语句写法 七种数据库中Select Top的使用方法
七种数据库中Select Top的使用方法 1. Oracle数据库 SELECT * FROM TABLENAME WHERE ROWNUM <= N 2. Infomix数据库 SELECT ...
- vim 查找命令
/要查找的内容 自光标起始位置向下查找 ?要查找的内容 自光标起始位置向上查找
- BZOJ-1043 [HAOI2008]下落的圆盘
几何题... 先把所有圆储存起来,然后对于每个圆我们求得之后放下的圆挡住了的部分,求个并集,并把没被挡到的周长加进答案. #include <cstdlib> #include <c ...
- 【13】react 之 redux(2)
转载:http://www.cnblogs.com/MuYunyun/p/6530715.html 什么情况需要用redux? 用户的使用方式复杂 不同身份的用户有不同的使用方式(比如普通用户和管理员 ...
- 【09】react 之 表单组件
不太清楚有多少初学React的同学和博主当时一样,在看完React的生命周期.数据流之后觉得已经上手了,甩开文档啪啪啪的开始敲了起来.结果...居然被一个input标签给教做人了. 故事是这样的:首先 ...
- 【05】Vue 之 实例详解与生命周期
Vue的实例是Vue框架的入口,其实也就是前端的ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进行对整个Vue实例生成.编译.挂着. ...
- hdoj 1175 连连看
连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- ckeditor与ckfinder的使用方法 .NET (转载)
原文发布时间为:2009-11-25 -- 来源于本人的百度文章 [由搬家工具导入] ckeditor与ckfinder的使用方法 .NET (转载) ckeditor 3.0.1学习笔记 一.ck ...
- ef code first transform,add ef power tools add-in,add tangible t4 editor for enhancement.
use ef power tools, as to .edmx file,right click at view, choose generate database from model, then ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---3
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: <Linux命令行与shell脚本 ...