我现在在做一个天气预报15的东西,调用API页面显示的是json数组,我想把js

学习安卓有一段时间了,应该提高自己的实战能力,做一些简单的Demo。下面我们介绍一下如何利用网络API实现天气预报功能,主要涉及到如何利用API获得网络数据,网络数据返回一般是JSON格式,这里又涉及到JSON的解析问题,这些都是比较基础的问题,应该予以掌握。
首先在找到你想要的API,这里我们选择,网页里有关于API的介绍:
接口地址:http://apistore.baidu.com/microservice/weather
请求方法:GET
请求参数:
citypinyin
请求示例:
http://apistore.baidu.com/microservice/weather?citypinyin=beijing
JSON返回示例:
errNum: 0,
errMsg: "success",
retData: {
city: "北京", //城市
pinyin: "beijing", //城市拼音
citycode: "",
//城市编码
date: "15-02-11", //日期
time: "11:00", //发布时间
postCode: "100000", //邮编
longitude: 116.391, //经度
latitude: 39.904, //维度
altitude: "33", //海拔
weather: "晴",
//天气情况
temp: "10", //气温
l_tmp: "-4", //最低气温
h_tmp: "10", //最高气温
WD: "无持续风向",
WS: "微风(&10m/h)", //风力
sunrise: "07:12", //日出时间
sunset: "17:44" //日落时间
下面搞一下布局文件:
version="1.0" encoding="utf-8"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:text="天气预报"
android:textSize="26dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:orientation="horizontal"
android:id="@+id/myedit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1111"
android:id="@+id/searchweather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="查询天气 "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="城市:"
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="天气:"
android:id="@+id/weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="实时气温:"
android:id="@+id/temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="最低气温:"
android:id="@+id/h_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="最高气温:"
android:id="@+id/l_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
下面连接工具类:
package org.lxh.
import java.io.BufferedR
import java.io.IOE
import java.io.InputStreamR
import java.net.HttpURLC
import java.net.URL;
public class HttpDownloader {
private URL url = null;
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
urlConn.setRequestMethod("GET");
urlConn.setConnectTimeout(8000);
urlConn.setReadTimeout(8000);
buffer = new BufferedReader(new InputStreamReader(
urlConn.getInputStream()));
while ((line = buffer.readLine()) != null) {
sb.append(line);
} catch (Exception e) {
e.printStackTrace();
} finally {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
return sb.toString();
获取返回字符串之后要对此JSON数据进行解析:
package org.lxh.
import java.util.ArrayL
import java.util.HashM
import java.util.L
import java.util.M
import org.json.JSONO
public class Util {
public List&Map&String, Object&& getInformation(String jonString)
throws Exception {
JSONObject jsonObject = new JSONObject(jonString);
JSONObject retData = jsonObject.getJSONObject("retData");
List&Map&String, Object&& all = new ArrayList&Map&String, Object&&();
Map&String, Object& map = new HashMap&String, Object&();
map.put("cityName", retData.optString("city"));
map.put("weather", retData.optString("weather"));
map.put("temp", retData.optString("temp"));
map.put("l_temp", retData.optString("l_tmp"));
map.put("h_temp", retData.optString("h_tmp"));
all.add(map);
下面Activity程序:
package org.lxh.
import java.util.I
import java.util.L
import java.util.M
import android.annotation.SuppressL
import android.app.A
import android.app.ProgressD
import android.os.B
import android.os.H
import android.os.M
import android.util.L
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.EditT
import android.widget.TextV
public class Main extends Activity {
private EditText citynameEditT
private Button searchWeatherB
private TextView citynametTextV
private TextView weahterTextV
private TextView tempTextV
private TextView h_tempTextV
private TextView l_tempTextV
String jonS
ProgressDialog progressD
private static final int SET = 1;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
Util util = new Util();
List&Map&String, Object&& all = util
.getInformation(msg.obj.toString());
Iterator&Map&String, Object&& iterator = all.iterator();
while (iterator.hasNext()) {
Map&String, Object& map = iterator.next();
Log.d("天气", map.get("weather").toString());
citynametTextView.setText(map.get("cityName")
.toString());
weahterTextView.setText(map.get("weather").toString());
tempTextView.setText(map.get("temp").toString());
h_tempTextView.setText(map.get("l_temp").toString());
l_tempTextView.setText(map.get("h_temp").toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
citynameEditText = (EditText) findViewById(R.id.myedit);
searchWeatherButton = (Button) findViewById(R.id.searchweather);
citynametTextView = (TextView) findViewById(R.id.city);
weahterTextView = (TextView) findViewById(R.id.weather);
tempTextView = (TextView) findViewById(R.id.temp);
h_tempTextView = (TextView) findViewById(R.id.h_temp);
l_tempTextView = (TextView) findViewById(R.id.l_temp);
searchWeatherButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new Thread(new NewThread()).start();
Log.d("按键", "Success");
private class NewThread implements Runnable {
public void run() {
String address = "http://apistore.baidu.com/microservice/weather?citypinyin="
+ citynameEditText.getText().toString();
HttpDownloader httpDownloader = new HttpDownloader();
String jonString = httpDownloader.download(address);
Message msg = Main.this.handler
.obtainMessage(Main.SET, jonString);
Main.this.handler.sendMessage(msg);
运行实例效果:
论坛回复 /
(0 / 4873)
浏览: 512 次
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'2014年10月 Java大版内专家分月排行榜第二2014年9月 Java大版内专家分月排行榜第二2014年8月 Java大版内专家分月排行榜第二
2016年1月 Java大版内专家分月排行榜第三2014年12月 Java大版内专家分月排行榜第三2014年11月 Java大版内专家分月排行榜第三
2016年1月 Java大版内专家分月排行榜第二2015年12月 Java大版内专家分月排行榜第二2015年8月 Java大版内专家分月排行榜第二2015年3月 Java大版内专家分月排行榜第二2015年1月 Java大版内专家分月排行榜第二2014年12月 Java大版内专家分月排行榜第二2014年11月 Java大版内专家分月排行榜第二2014年6月 Java大版内专家分月排行榜第二2014年4月 Java大版内专家分月排行榜第二2014年1月 Java大版内专家分月排行榜第二2013年11月 Java大版内专家分月排行榜第二
2015年9月 Java大版内专家分月排行榜第三2015年6月 Java大版内专家分月排行榜第三2015年5月 Java大版内专家分月排行榜第三2015年2月 Java大版内专家分月排行榜第三2014年3月 Java大版内专家分月排行榜第三2013年12月 Java大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。使用 PHP 获取并解析 JSON 显示在页面中
我的图书馆
使用 PHP 获取并解析 JSON 显示在页面中
PHP 获取接口内容你如果想解析 JSON 数据并且显示在页面中,第一步肯定要先得到 JSON 接口文件的内容。在 PHP 中获取一个页面的内容,可以使用&远程页面然后使用循环获取内容。假设接口文件页面为:http://www.qttc.net/api.php?action=open_getBlogList&only_recommend=1&limit=5 ,那么我们可以使用下面语句获取这个接口文件内容:$handle = fopen("http://www.qttc.net/api.php?action=open_getBlogList&only_recommend=1&limit=5","rb");
$content = "";
while (!feof($handle)) {
&& &$content .= fread($handle, 10000);
fclose($handle);这样 content 保存的就是 JSON api 内容。PHP 解析 JSON 并显示原始的内容是无法直接调用的,必须被 PHP 进行进一步处理,才能被调用显示在网页中。在 PHP 5.2 及后续版本中,使用 json_decode() 函数来解析 JSON 数据,将其转换成 PHP 可以调用的数据格式。例如:$content = json_decode($content);解析之后呢,我们就可以按照 PHP 中调用数组数据的方法一样的调用 JSON 中的数据。这个调用方法需要按照具体的 JSON 数据格式来写,演示请看下面。关于 json_decode 函数的使用,具体看 PHP 手册,这里不再赘述:实战调用琼台博客的 api细心的朋友会发现&的边栏最下面多了一个“友文推荐”模块,里面推荐了一些的文章。友文推荐是琼台博客倡议的一种博客之间交流方式,比传统的友情链接更有效,同时实现了博客内容互补。由于琼台博客的博客程序是他自己本人编写的,所以他提供了 JSON api 接口,可以获取到最新的可推荐的文章。本人使用 PHP 获取这个 JSON 接口,然后输出到自己博客的边栏中,下面来实战操作一下。第一步,查看 api 调用方式调用之前,你肯定要看一下对方的 api 调用手册,包括调用地址、如何调用、数据输出格式等等。琼台博客的 api 说明地址如下:。根据文档,我使用了 http://www.qttc.net/api.php?action=open_getBlogList&only_recommend=1&limit=5 这样的参数,意思就是调用五条他推荐的文章。第二步,获取 api 结构数据很简单,上面说过了,具体代码如下:$handle = fopen("http://www.qttc.net/api.php?action=open_getBlogList&only_recommend=1&limit=5","rb");
$content = "";
while (!feof($handle)) {
&& &$content .= fread($handle, 10000);
fclose($handle);先打开这数据文件,然后把所有内容保存到 content 变量中,因为可以肯定 api 数据不会超过 10000个字符,所以用 10000 作为 fread 函数的第二个参数。这样,api 返回的 JSON 数据就保存在了 content 变量中。第三步,解析并输出内容使用下面代码解析数据,然后调用输出$content = json_decode($content);
foreach ($content-&data as $key) {
&& &echo '&li&&a target="_blank" href="'.$key-&b_url.'"&'.$key-&b_title.'&/a&&/li&';
}首先对 content 变量中的 JSON& 数据处理,然后变成 PHP 可以调用的数据,再使用 foreach 遍历输出这五条内容,按照我需要的 HTML 格式,将内容插入进去即可。再加上样式修饰,这样就完成了 获取并解析 JSON 显示在页面中了。调用其它 api 数据的方法大同小异
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&[转]&
喜欢该文的人也喜欢986被浏览279,129分享邀请回答25729 条评论分享收藏感谢收起thinkpage.cn/2. 彩云天气API2.1 免费版:1000次/天。全球城市数据,天气实况,2天预报,没有生活指数。他们的网站很久没有动静了……2.2 收费版:可试用。彩云主要是做降雨预报的,能预报两个小时内的分钟级降雨(貌似是独家数据,不确定准不准),有to C的app。其他天气数据不是很全。而且连每个月4000的套餐里都没有生活指数、气象灾害预警之类的数据,性价比很低。没试过定制,不知道定制版有没有。网址:3. 和风天气API3.1 免费版:3000次/天。全球城市数据,天气实况,7天预报,没有生活指数。3.2 收费版:不能试用。这家API的收费版特别鸡贼,套餐价写着“99元起”,其实只要买数据就不止99元。总的来说定价比心知、彩云便宜,但要注意他家访问量是单独卖的,基础套餐里一天访问量只有1万,访问量大划不来。网站有点low。网址:4. 聚合数据API。聚合是个整合平台,上面天气API一共有7个,每个价格不同。4.1 免费版:有1个天气预报接口免费。其余6个都是收费的。4.2 收费版:7个都不贵,但都只提供单一数据,没有能完整提供所有天气数据的接口,比较散乱。
访问量都只有一点点,也要单独买。
看到联系方式是邮箱,不知道出问题了找不找得到人管。网址:5. 丫丫天气API。5.1 免费版:30次/小时,也就是720次/天。天气实况,2天预报,没有生活指数。5.2 收费版:不能试用。按城市数量分三个等级,国际城市数量比较少。套餐里本身包含的访问量少,要另买。数据比较便宜,访问量另买贵,估计主要是赚访问量的钱。地址:还有一些国外的API,有收费的有免费的,大家可以自己试试。不过国外API的中国数据应该也是从气象局扒的,准确度不一定有国内API高。weather underground:aeris weather:open weather map:678 条评论分享收藏感谢收起

我要回帖

更多关于 天气预报 的文章

 

随机推荐