免费天气预报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. C#中的is和as的转型区别

    摘自CLR via C#第三版第四章 在c#中is可以用来判断一个对象是否兼容给定的类型,如果是返回true,否则返回false. 同时is是永不会抛出异常的.如果对象引用是null,is操作符总是返 ...

  2. 【bzoj2529】[Poi2011]Sticks 贪心

    题目描述 给出若干木棍,每根木棍有特定的颜色和长度.问能否找到三条颜色不同的木棍构成一个三角形.(注意这里所说的三角形面积要严格大于0) 输入 第一行给出一个整数k(3<=k<=50),表 ...

  3. C++ essentials 之 explicit constructor

    这篇博客的源起是我下面的一段代码 #include <bits/stdc++.h> using namespace std; int main(){ priority_queue<l ...

  4. 【11】react 之 flux

    Flux 是 Facebook 使用的一套前端应用的架构模式.React 标榜自己是 MVC 里面 V 的部分,那么 Flux 就相当于添加 M 和 C 的部分. 1.1.  Flux介绍 Flux并 ...

  5. bat文件【java调用】

    Runtime.getRuntime().exec("cmd /c del c:\\a.doc");   //Runtime.getRuntime().exec("not ...

  6. Java中的IO基本用法

    先贴一下我在作业中用到的三种文件输入辅助类.三种文件输出辅助类 public class BuffIn implements InHelp{ private BufferedReader buffer ...

  7. java网络编程学习笔记(四):线程池的实现

    package QQ; import java.util.LinkedList; /** * Created by hu on 2015/11/9. */ public class ThreadPoo ...

  8. Linux(ubuntu)下手动安装 firefox 6 并且添加快捷方式图标

    Mozilla 正式发布了Firefox 6,如果你的电脑上还在用非常古老的版本么,赶紧过来更新下吧,由于官网上面只是提供了linux下的.bz2的压缩包,没有deb或者rmp格式,所以需要自己安装下 ...

  9. 《手把手教你学C语言》学习笔记(3)---变量

    编程目的是为了解决问题,编程本质是用计算机的思维操作数据,操作就是算法,数据主要是数据类型,也可以说量,其中分为常量和变量,常量主要是指在量的生命周期内无法改变其值:变量主要是指在量的生命周期内可以随 ...

  10. maven更换阿里云仓库

    本来不想写,网上到处都是,不过好多到我这不行,自己记录下,省的到处找 D:\apache-maven-3.6.1\conf目录下setting.xml文件(这是我的解压的位置) <mirrors ...