原创 安卓从百度天气接口获取天气,Http和JSON操作

2015-12-3 21:49 2239 21 22 分类: 软件与OS 文集: 学以致用

安卓从百度天气接口获取天气,Http和JSON操作

wxleasyland@sina.com

2015.12

 

url中的ak=后面要加上百度的key,请自行申请。

 

关键代码如下,比较简单:

 

import org.json.JSONArray;

import org.json.JSONObject;

import android.os.Handler;

 

import java.net.URL;

import java.net.HttpURLConnection;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.BufferedReader;

 

// 百度天气接口JSON示例     String g_str="{\"error\":0,\"status\":\"success\",\"date\":\"2015-11-17\",\"results\":[{\"currentCity\":\"北京\",\"pm25\":\"77\",\"index\":[{\"title\":\"穿衣\",\"zs\":\"较冷\",\"tipt\":\"穿衣指数\",\"des\":\"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。\"},{\"title\":\"洗车\",\"zs\":\"不宜\",\"tipt\":\"洗车指数\",\"des\":\"不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。\"},{\"title\":\"旅游\",\"zs\":\"适宜\",\"tipt\":\"旅游指数\",\"des\":\"天气较好,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,适宜旅游。\"},{\"title\":\"感冒\",\"zs\":\"少发\",\"tipt\":\"感冒指数\",\"des\":\"各项气象条件适宜,无明显降温过程,发生感冒机率较低。\"},{\"title\":\"运动\",\"zs\":\"较不宜\",\"tipt\":\"运动指数\",\"des\":\"天气较好,但考虑天气寒冷,推荐您进行室内运动,户外运动时请注意保暖并做好准备活动。\"},{\"title\":\"紫外线强度\",\"zs\":\"最弱\",\"tipt\":\"紫外线强度指数\",\"des\":\"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。\"}],\"weather_data\":[{\"date\":\"周二 11月17日 (实时:7℃)\",\"dayPictureUrl\":\"http://api.map.baidu.com/images/weather/day/duoyun.png\",\"nightPictureUrl\":\"http://api.map.baidu.com/images/weather/night/yujiaxue.png\",\"weather\":\"多云转雨夹雪\",\"wind\":\"微风\",\"temperature\":\"11 ~ 2℃\"},{\"date\":\"周三\",\"dayPictureUrl\":\"http://api.map.baidu.com/images/weather/day/yin.png\",\"nightPictureUrl\":\"http://api.map.baidu.com/images/weather/night/yin.png\",\"weather\":\"阴\",\"wind\":\"微风\",\"temperature\":\"5 ~ 3℃\"},{\"date\":\"周四\",\"dayPictureUrl\":\"http://api.map.baidu.com/images/weather/day/yujiaxue.png\",\"nightPictureUrl\":\"http://api.map.baidu.com/images/weather/night/yujiaxue.png\",\"weather\":\"雨夹雪\",\"wind\":\"微风\",\"temperature\":\"5 ~ 2℃\"},{\"date\":\"周五\",\"dayPictureUrl\":\"http://api.map.baidu.com/images/weather/day/yujiaxue.png\",\"nightPictureUrl\":\"http://api.map.baidu.com/images/weather/night/xiaoyu.png\",\"weather\":\"雨夹雪转小雨\",\"wind\":\"微风\",\"temperature\":\"4 ~ 1℃\"}]}]}";

 

 

    void mygeneratestr()  //启动一个线程,进行http连接,整理天气数据

    {

       new Thread(new Runnable()

       {

           @Override 

           public void run()

           { 

 

              g_str="";

              g_str2="";

              g_str3="";

              g_str4="";

              String str="";

             

              HttpURLConnection connection=null; 

              try

              { 

                  URL url=new URL("http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak="); 

                  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 sb=new StringBuilder(); 

                  while((str=reader.readLine())!=null)      sb.append(str); 

                  g_str+=sb.toString();

              }

              catch(Exception e)

              { 

                  //e.printStackTrace(); 

                  g_str=null;

                  g_str2="http出错。错误信息:"+e.toString();

              }

              finally

              { 

                  if(connection!=null) connection.disconnect(); 

              }

             

             

              if(g_str==null) { g_str=""; return;}

              if(!g_str.contains("\"status\":\"success\""))

              {

                  g_str2="http得到的信息不符:"+g_str;

                  g_str="";

                  return;

              }

             

              try

              {

                  JSONObject myobj=new JSONObject(g_str);  //总OBJECT

                  JSONObject myobj2=myobj.getJSONArray("results").getJSONObject(0);

                  JSONObject myobj31=myobj2.getJSONArray("index").getJSONObject(0);//穿衣

                  JSONObject myobj32=myobj2.getJSONArray("index").getJSONObject(1);//洗车

                  JSONObject myobj33=myobj2.getJSONArray("index").getJSONObject(2);//旅游

                  JSONObject myobj34=myobj2.getJSONArray("index").getJSONObject(3);//感冒

                  JSONObject myobj35=myobj2.getJSONArray("index").getJSONObject(4);//运动

                  JSONObject myobj36=myobj2.getJSONArray("index").getJSONObject(5);//紫外线

                  JSONObject myobj41=myobj2.getJSONArray("weather_data").getJSONObject(0);//今

                  JSONObject myobj42=myobj2.getJSONArray("weather_data").getJSONObject(1);//明

                  JSONObject myobj43=myobj2.getJSONArray("weather_data").getJSONObject(2);//后

                  JSONObject myobj44=myobj2.getJSONArray("weather_data").getJSONObject(3);//大后

                 

                  g_str2=myobj2.getString("currentCity")+myobj41.getString("date")+"指数"+myobj2.getString("pm25")+"\n"

                         +"今天 "+myobj41.getString("temperature")+" "+myobj41.getString("weather")+" "+myobj41.getString("wind")+"\n"

                         +"明天 "+myobj42.getString("temperature")+" "+myobj42.getString("weather")+" "+myobj42.getString("wind")+"\n"

                         +"后天 "+myobj43.getString("temperature")+" "+myobj43.getString("weather")+" "+myobj43.getString("wind")+"\n"

                         +"大后 "+myobj44.getString("temperature")+" "+myobj44.getString("weather")+" "+myobj44.getString("wind")+"\n"

                         +myobj31.getString("des");

                  //g_str3=myobj31.getString("des")+"\n"

                  //     +myobj34.getString("des")+"\n"

                  //     +myobj36.getString("des");

                  g_str4="今"+myobj41.getString("temperature")+"\n"

                         +"明"+myobj42.getString("temperature");

                 

              }

              catch (Exception e)

              {

                  g_str="";

                  g_str2 ="http正常,但JSON 出错";

                  g_str3 ="";

                  g_str4 ="";

                  //e.printStackTrace();

              }

           } 

       }).start();

    }

 

 

文章评论1条评论)

登录后参与讨论

用户593939 2015-12-7 22:23

写的好,有道理,学习了
相关推荐阅读
wxleasyland 2016-06-23 20:35
简单翻译W25Q64BV数据手册(Winbond串行闪存SPI总线)
百度文库 http://wenku.baidu.com/view/7bfd82fd5901020206409c1b...
wxleasyland 2016-06-22 17:33
安卓手机中加入busybox命令,打包tar,HC-KTOOL备份EFS的efs.tar.gz长度为0解决
安卓手机中加入busybox命令,打包tar,HC-KTOOL备份EFS的efs.tar.gz长度为0解决 wxleasyland@sina.com 2016.6.17 I9300手机,4....
wxleasyland 2016-06-19 21:17
电脑机箱USB扩展面板失灵原因查找
wxleasyland@sina.com 2016.6   山寨电脑机箱前面的2个USB口扩展面板,是通过排线接到主板上的插座的。 用得好好的,中间有搞了搞电脑,后来就发现有一个...
wxleasyland 2016-06-17 13:44
I9300手机解锁亮屏慢,是Exynos处理器的原因
I9300手机解锁亮屏慢,是Exynos处理器的原因 三星I9300手机,全新刷的官方系统,没有装任何软件。 按电源键或HOME键,亮屏慢,需要1~2秒屏幕才亮起来,找遍网上,没有解法。 后...
wxleasyland 2016-06-16 13:48
华硕主板FW status recovery error故障修复,双BIOS功能分析
华硕主板FW status recovery error故障修复,双BIOS功能分析 wxleasyland@sina.com 2016.6   最近买了一个二手华硕主板P8B75...
wxleasyland 2016-05-01 19:47
WINDOWS(WIN7等)用U盘安装方便(非WINPE)、XP需PE
WINDOWS(WIN7等)用U盘安装方便(非WINPE)、XP需PE 2016年5月1日     一、在WINDOWS中安装WINDOWS 在已运行的WINDOWS中,点击硬...
我要评论
1
21
关闭 站长推荐上一条 /2 下一条