1. 程式人生 > >Android天氣預報的製作

Android天氣預報的製作

相信很多人在剛接觸Android時都會做一個天氣預報來了解Android,今天得空整理了一個簡單天氣預報Demo的製作,如下:

首先我們應該做的便是搞到一個天氣預報的API,在這裡我推薦註冊和風天氣個人開發者,認證時間1-3天,基本一下子就可以收到了認證成功的郵件。認證開發者後可以獲得中國和海外城市的7天預報、逐3小時預報、實況天氣、空氣質量和生活指數,對比了一下其它家,和風真是算良心介面了,雖然我只需要實況天氣這一個資料~~~~

和風天氣API網址https://www.heweather.com/douments/api/s6/weather-forecast  各位看官可以去裡面註冊獲得key就可以開發繼續啦

1、配置引數

        既然涉及到了網路請求,肯定應該想到的時許可權所以我們先添加個許可權,在AndroidManifest中新增

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

        然後我這裡使用的是okhttp所以新增okhttp的依賴,在app下的build.gradle中的dependencies下新增

 compile 'com.squareup.okhttp3:okhttp:3.6.0'

2、好了,該配置的都配置完了。我們接下來就製作佈局吧。佈局我就截個圖大家可以自由製作

         

3、製作完佈局後,我們就繼續回到Java程式碼中首先先初始化各種控制元件這個就不過多說明

         

4、接下來開始請求介面了,這裡我是個按鈕新增的點選事件然後呼叫的search方法如下:

public void search() {
    Log.d(TAG, "search: 1111");
    OkHttpClient client = new OkHttpClient();
    String uri = "https://free-api.heweather.com/s6/weather?location=" + cityItem.getSelectedItem().toString() + "&key=你在和風天氣上申請的key";
    Request request = new Request.Builder()
            .url(uri)
            .build();
    final Call call = client.newCall(request);
    new Thread(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "run: 222222");
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.d(TAG, "onResponse: " + e);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Message message=new Message();
                    message.what=1;
                    message.obj=response.body().string();
                    handler.sendMessage(message);
                }
            });
        }
    }).start();

}

        這裡我們是先將城市和key拼接一個GET請求,然後封裝至okhttp的Call中開啟執行緒去請求,大家千萬要注意Android的禁忌那就是不能在子執行緒更新UI,這個是千萬不能去碰的,所以我這裡使用了handler的方法回到主執行緒更新UI。

5、handler的使用,首先我們需要建立一個全域性的handler來接受子執行緒或其他地方傳來的訊息

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (msg.what==1){
            String result=msg.obj.toString();
            updateUI(result);
            Log.d(TAG, "handleMessage: "+result);
        }

    }
};

 這裡面的msg.what==1就是一個識別符號,代表你從哪傳來的我們在請求成功後就傳送訊息,上面程式碼子執行緒中便有寫到,如下:

          

   先製作一個Message訊息然後將介面請求後的資料放入message中發出,我們就可以在之前定義的全域性handler中去處理這個結果,在裡面呼叫更新UI的方法。

6、更新UI,我們先來看一下介面返回的資料吧

        

       他這裡返回的JSON是一層套一層,不過我們不要著急,一步一步的來解析,在這個專案中我使用的是Android自帶的json工具來解析的,好了,我們繼續。我們就已拿 "now" 現在的資料舉例吧。

      首先我們看到最外層的 "HeWeather6" ,它的後面是一個 [ ] 包含的資料,所以我們可以得知他是一個JSONArray於是外面先用JSONArray來獲取它裡面的資料

JSONArray jsonArray=new JSONObject(result).getJSONArray("HeWeather6");

      我們進去之後呢就看到了一個 { } 的符號將所有資料都包攬起來了這就代表了只是一個JSONObject,我們再來用剛剛獲取到的jsonarray來獲取這個JSONObject,如下:

JSONObject jsonObject=jsonArray.getJSONObject(0);

      好了現在我們已經到很裡面了,可以看到都是一個 " "加 { } 的結構這代表這他們都是有名字的JSONObject,我們繼續來獲取我們想要的 "now" 

JSONObject now=jsonObject.getJSONObject("now");

     OK,我們已經獲取到了,接下來就把它裡面的一個個資料放到頁面中吧。更新UI的詳細方法如下:

public void updateUI(String result){
    try {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        timeText.setText(simpleDateFormat.format(new Date()));
        JSONArray jsonArray=new JSONObject(result).getJSONArray("HeWeather6");
        JSONObject jsonObject=jsonArray.getJSONObject(0);
        JSONObject now=jsonObject.getJSONObject("now");
        cityText.setText(cityItem.getSelectedItem().toString());
        nowWeather.setText(now.get("cond_txt")+"");
        nowTmp.setText(now.get("tmp")+"℃");
        nowWindDir.setText(now.get("wind_dir")+"");
        nowWindSc.setText(now.get("wind_sc")+"級");
        JSONArray jsonArray1=jsonObject.getJSONArray("daily_forecast");
        JSONObject todayJson= (JSONObject) jsonArray1.get(0);
        tmpMaxText.setText(todayJson.get("tmp_max")+"℃");
        tmpMinText.setText(todayJson.get("tmp_min")+"℃");
        dayTimeText.setText(todayJson.get("cond_txt_d")+"");
        nightText.setText(todayJson.get("cond_txt_n")+"");
        windDirText.setText(todayJson.get("wind_dir")+"");
        windScText.setText(todayJson.get("wind_sc")+"級");
        JSONObject tomorrowJson= (JSONObject) jsonArray1.get(1);
        tomorrowTmpText.setText(tomorrowJson.get("cond_txt_d")+"");
        tomorrowCondText.setText(tomorrowJson.get("tmp_min")+"-"+tomorrowJson.get("tmp_max")+"℃");
        Log.d(TAG, "updateUI: "+todayJson);
        Log.d(TAG, "updateUI: "+jsonArray);
        Log.d(TAG, "updateUI: "+now);
    } catch (JSONException e) {
        e.printStackTrace();
    }

   到這裡我們就已經全部完成了,大家有什麼不懂的可以留言,我會及時回覆的。最後將原始碼放上,以供大家參考。Android天氣預報Demo原始碼