免费天气预报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解析聚合数据之天气预报的更多相关文章

  1. 聚合数据全国天气预报api接口

    查询天气预报在APP中常用的一个常用功能,聚合数据全国天气预报api接口可以根据根据城市名/id查询天气.根据IP查询天气.据GPS坐标查询天气.查询城市天气三小时预报,并且支持全国不同城市天气预报查 ...

  2. Android通过聚合数据API实现天气预报

    使用聚合数据的API 聚合数据地址:https://www.juhe.cn/ 在数据服务->生活常用->全国天气预报,申请天气预报的API使用的KEY 保存请求示例的地址,把您申请的KEY ...

  3. 聚合数据全国天气预报API--ajax 通过城市名取数据

    聚合数据天气预报API:https://www.juhe.cn/docs/api/id/39 接口地址:http://v.juhe.cn/weather/index 支持格式:json/xml 请求方 ...

  4. Android解析Json数据之Gson解析

    Gson是谷歌官方提供的解析json数据的工具类.json数据的解析能够使用JSONObject和JSONArray配合使用解析数据,可是这样的原始的方法对于小数据的解析还是有作用的,可是陪到了复杂数 ...

  5. android 解析服务器数据使用json还是xml方式

    整理自百度搜索: 现在的Android应用程序,几乎没有不与服务端交换数据的了!那么,android应用在与服务端交换数据的时候,我们有哪些选择呢?哪种数据交换格式要更好吗?下面文章简单为 andro ...

  6. Android解析json数据

    Json数据 [{"code":"110000","sheng":"11","di":"0 ...

  7. [转]Android解析json数据

    1.json格式 2.json解析 3.gson解析 4.fastjson解析 一.Json格式 json一种轻量级的数据交换格式.在网络上传输交换数据一般用xml, json. 两种结构: 1)对象 ...

  8. Android解析中国天气接口JSon数据,应用于天气查询!

    android解析Json数据是比较常见的一种操作.也是客户端和服务器进行数据交互的桥梁.下面就来看一看在android中解析JSon数据的方法吧. 首先要想获得Json数据,就必须访问相关的网络接口 ...

  9. android基础---->JSON数据的解析

    上篇博客,我们谈到了XML两种常用的解析技术,详细可以参见我的博客(android基础---->XMl数据的解析).网络传输另外一种数据格式JSON就是我们今天要讲的,它是比XML体积更小的数据 ...

随机推荐

  1. table中填写数据并批量增加

    <table class = "table jtable table-bordered table-striped hide" id = "table_1" ...

  2. Fabric 和 Sawtooth 技术分析(下)

    http://blog.talkingdata.com/?p=6172 在前一篇文章(Fabric和Sawtooth技术分析(上))中,我们着重跟大家分享了 Fabric 相关的内容,在本篇文章中,我 ...

  3. C++ Contest Code preprocessor

    大概可以拿来方便拉模板 变量名.语法都是瞎整的你感觉有用随便用好了.. #include<bits/stdc++.h> using namespace std; map<string ...

  4. numeric 转换为数据类型 (null) 时出现算术溢出错误

    mssql数据同步到mysql时提示错误如下: 消息 8115,级别 16,状态 14,第 1 行 将 numeric 转换为数据类型 (null) 时出现算术溢出错误 问题分析如下: 1.数据字段类 ...

  5. 多表的时候怎样在MVC VIEW中显示

    原文发布时间为:2011-04-01 -- 来源于本人的百度文章 [由搬家工具导入] Linq join query displayed in MVC view Instead of returnin ...

  6. 如何查看用pip命令安装的软件的安装路径

    怎么查看python的pip install xx安装的软件的安装路径? pip freeze命令可以查看用pip安装的软件有哪些 要查看安装路径,在执行一次命令pip install xx,就会告诉 ...

  7. IMAGE_OPTIONAL_HEADER32 结构作用

    IMAGE_OPTIONAL_HEADER32 结构作用 接 着我们来谈谈 IMAGE_OPTIONAL_HEADER 结构,正如名字的意思,这是一个可选映像头,是一个可选的结构,但是呢,实际上上节课 ...

  8. hdu 4956(思路题)

    Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. AC日记——第K大的数 51nod 1105

    1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...

  10. (2)Django-pycharm部署

    pycharm 1.新建一个Django工程 2.部署 在pycharm下方的terminal终端中输入命令: python manage.py startapp app1 生成“app1”文件夹 3 ...