android中国天气网api获取天气信息并解析

package doge.healthcare.Utils;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by doge on 2017/5/17.
 */

//初始化天气数据
public class WeatherUtil {
    private String cloth= null ,cold= null,light= null,sport= null,city= null,weather_toady=null,weather_tomorror = null,weather_houtian=null,weather_dahoutian=null,wind_toady=null,wind_tomorror = null,wind_houtian=null,wind_dahoutian=null;
    public  WeatherUtil  (final Handler handler_setDate) throws JSONException {
        new Thread(new Runnable() {
            @Override

            public void run() {
                String strURL = "http://jirenguapi.applinzi.com/weather.php";
                URL url = null;
                try {
                    url = new URL(strURL);

                    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
                    InputStreamReader input = new InputStreamReader(httpConn
                            .getInputStream(), "utf-8");
                    BufferedReader bufReader = new BufferedReader(input);
                    String line = "";
                    StringBuilder contentBuf = new StringBuilder();
                    while ((line = bufReader.readLine()) != null) {
                        contentBuf.append(line);
                    }
                    //通过字符串,获得最外部的json对象
                    JSONObject jsonObj = new JSONObject(contentBuf.toString());
                    //获得json对象数组
                    JSONArray arr = jsonObj.getJSONArray("results");

                    for (int i = 0; i < arr.length(); i++) {
                        //循环对象,并通过getString("属性名");来获得值
                        switch (i) {
                            case 0: {
                                //获得城市名
                                JSONObject tempJson = arr.getJSONObject(0);
                                city = tempJson.getString("currentCity");
                            }
                            case 1: {
                                //获取穿衣,日照等数据
                                JSONObject tempJson = arr.getJSONObject(0);
                                JSONArray indexarr = tempJson.getJSONArray("index");//获取index里的数据
                                JSONObject clothjson = indexarr.getJSONObject(0); //穿衣
                                JSONObject coldjson = indexarr.getJSONObject(2); //感冒
                                JSONObject sportjson = indexarr.getJSONObject(3); //运动
                                JSONObject lightjson = indexarr.getJSONObject(4); //穿衣
                                cloth = clothjson.getString("des");
                                cold = coldjson.getString("des");
                                sport = sportjson.getString("des");
                                light = lightjson.getString("des");
                            }
                            case 2: {
                                //当天天气
                                JSONObject tempJson = arr.getJSONObject(0);
                                JSONArray indexarr = tempJson.getJSONArray("weather_data");//获取近几天天气数据
                                JSONObject todayjson = indexarr.getJSONObject(0); //今天
                                JSONObject tomrrowjson = indexarr.getJSONObject(1); //明天
                                JSONObject houtianjson = indexarr.getJSONObject(2); //后天
                                JSONObject dahoutianjson = indexarr.getJSONObject(3); //大后天
                                weather_toady = todayjson.getString("date") + "\n" +todayjson.getString("weather")+ "\n" +todayjson.getString("temperature");
                                weather_tomorror = tomrrowjson.getString("date") + "\n"+ tomrrowjson.getString("weather") + "\n"+ tomrrowjson.getString("temperature");
                                weather_houtian = houtianjson.getString("date") + "\n"+ houtianjson.getString("weather")+ "\n" + houtianjson.getString("temperature");
                                weather_dahoutian = dahoutianjson.getString("date") + "\n" + dahoutianjson.getString("weather")+ "\n"+ dahoutianjson.getString("temperature");
                                wind_toady = todayjson.getString("wind");
                                wind_tomorror = tomrrowjson.getString("wind");
                                wind_houtian = houtianjson.getString("wind");
                                wind_dahoutian = dahoutianjson.getString("wind");
                                //完成后发回一个消息表示完成
                                handler_setDate.sendEmptyMessage(0);
                            }
                        }

                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    public String getTemperature ()
    {
        return "城市:"+"\n"+city+
                "\n今日:"+weather_toady+
                "\n明天"+"\n"+weather_tomorror+
                "\n后天"+"\n"+weather_houtian+
                "\n大后天"+"\n"+weather_dahoutian+
                "\n"+
                "穿衣指数:\n"
        +cloth;
    }
    public String getWet()
    {
        return "风力指数\n"+
                wind_toady+"\n"+
                "感冒指数:\n"+
                cold+"\n"
                +"运动指数\n"+
                sport;
    }

    public String getLight()
    {
        return "紫外线指数\n"+
                light
                ;
    }

}

使用方法如下:


                String temperaturetext= weatherUtil.getTemperature();
                String temperaturetext2= weatherUtil.getWet();
                String temperaturetext3= weatherUtil.getLight();
          
用getTemperature ,getwet等方法即可获得当天天气等各项数据


最后贴一下效果图

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值