调用天气预报api

    进入和风天气网主页   http://heweather.com/  ,查看API文档可发现我们需要的天气预报指数数据挺全面的,下面以获取当天的最高温度,最低温度,当前温度,PM2.5,空气质量,紫外线强度,湿度为例:

   布局界面比较简单,一个按钮和显示文本,点击按钮获取天气指数在文本框里显示

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

            android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="获取天气情况" />

            android:id="@+id/txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="天气数据:" />


以北京的天气为例:


package com.example.dfd;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView txtTextView;
    Button btn;
    String result = “; 
    String target="https://api.heweather.com/x3/weather?cityid=CN101010100&key=af71e684a8f540feaaad7c92566324c1";
    URL url;
    Handler handler;
   WeatherContent content;   

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         btn = (Button)findViewById(R.id.btn);
         txtTextView = (TextView)findViewById(R.id.txt);

         content = new WeatherContent();      

         btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    
                    @Override
                    public void run() {
                        send();
                        Message message = handler.obtainMessage();
                        handler.sendMessage(message);
                    }
                }).start();
            }
        });
         handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(result!=null){
                    txtTextView.setText(jiexi());
                }
            }
        };
    }
    public void send(){
        try {
            url = new URL(target);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            InputStream is = connection.getInputStream();
            BufferedReader buffered = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String input = null;
            while ((input = buffered.readLine())!=null) {
               result+=input+"\n";
            }
            buffered.close();
            connection.disconnect();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 private String jiexi() {
        try {
            JSONArray jsonObjs = new JSONObject(result).getJSONArray("HeWeather data service 3.0");
            JSONObject japi = ((JSONObject) jsonObjs.opt(0))
                    .getJSONObject("aqi");
            JSONObject japi_city = japi.getJSONObject("city");

            //PM2.5          

           String pm25 = japi_city.getString("pm25");
            // 空气质量
            String qlty = japi_city.getString("qlty");
            JSONObject jbasic = ((JSONObject) jsonObjs.opt(0))
                    .getJSONObject("basic");

             //城市名称

             String city = jbasic.getString("city");
            JSONArray jdaily_forecast = ((JSONObject) jsonObjs.opt(0))
                    .getJSONArray("daily_forecast");
            JSONObject jdaily_forecast_1 = ((JSONObject) jdaily_forecast.opt(0))
                    .getJSONObject("tmp");
            // 最高温度
            String max = jdaily_forecast_1.getString("max");
            String min = jdaily_forecast_1.getString("min");
            JSONObject jnow = ((JSONObject) jsonObjs.opt(0))
                    .getJSONObject("now");

            //当前温度

            String tmp = jnow.getString("tmp");
            // 湿度
            String hum = jnow.getString("hum");
           //天气描述
            String txtdescrible = jnow.getJSONObject("cond").getString("txt");
            content.setTxtdescrible(txtdescrible);
            JSONObject jsuggestion = ((JSONObject) jsonObjs.opt(0))
                    .getJSONObject("suggestion");

           //紫外线

           String ultraviolet = jsuggestion.getJSONObject("uv").getString("brf");
            return "城市:" + city +" ,PM2.5:" + pm25 + ",空气质量:" + qlty + " ,当前温度:" + tmp + ",湿度:" + hum
                    + ",天气情况:" + txtdescrible
                    + ",紫外线:" + ultraviolet + ",温度:" + min + "℃~" + max + "℃";

        } catch (JSONException ex) {

          ex.printStackTrace();      

    }
        return null;
    } 

你可能感兴趣的:(android)