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等方法即可获得当天天气等各项数据 
  


最后贴一下效果图android中国天气网api获取天气信息并解析_第1张图片

你可能感兴趣的:(android开发)