Android National Weather Forecast SDK Demo

What I want to share today is the scheme of integrating the weather forecast function for Android mobile devices. The solution I took was to use the National Weather Forecast API on aggregated data. If needed, you can apply: https://www.juhe.cn/docs/api/id/39

Implementation steps:

1. Register on the official website of aggregated data, and then apply for the SDK data of the national weather forecast to generate an AppKey.

2. Configure the project, copy juhe_sdk_v_1_1.jar in the development package to the libs root directory, and copy libJuheSDK_v_1_0.so to the libs\armeabi directory, as shown in the figure:

3. Find the following methods according to the interface documentation:

<span style="font-family:SimSun;font-size:18px;">/**  
     * Query weather by city name  
     *   
     * @param cityname  
     * City name, such as: "Suzhou"  
     * @param format  
     * There are two return formats for the next 6 days forecast (future), 1 or 2, the default is 1  
     *  
     * @param jsonCallBack  
     *   
     */  
    public void getByCitys(String cityname, int format, JsonCallBack jsonCallBack) /**  
     * List of weather types and signs  
     *   
     * @param jsonCallBack  
     *   
     */  
    public void getUni(JsonCallBack jsonCallBack) /**  
     *   
     * Query weather by IP  
     *   
     * @paramip  
     * ip address, such as: 58.215.185.154  
     * @param format  
     * There are two return formats for the next 6 days forecast (future), 1 or 2, the default is 1  
     * @param jsonCallBack  
     *   
     */  
    public void getByIP(String ip, int format, JsonCallBack jsonCallBack)/**  
     *   
     * Query weather based on GPS coordinates  
     *   
     *   
     * @param lon  
     * Longitude  
     * @param lat  
     * Latitude  
     * @param format  
     * There are two return formats for the next 6 days forecast (future), 1 or 2, the default is 1  
     * @param jsonCallBack  
     *   
     */  
    public void getByGEO(double lat, double lon, int format, JsonCallBack jsonCallBack) /**  
     * Three hour forecast of city weather  
     *   
     * @param cityname  
     * @param jsonCallBack  
     *   
     */  
    public void getForecast3h(String cityname, JsonCallBack jsonCallBack) /**  
     * List of supported cities  
     *   
     * @param jsonCallBack  
     */  
    public void getCities(JsonCallBack jsonCallBack)</span>  

 4. Call the method in the program 

First, add permissions in AnroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" />  
    <uses-permission android:name="android.permission.INTERNET" />  
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
    <uses-permission android:name="android.permission.READ_CONTACTS" />  

 Add your applied openid inside the application tag

<meta-data  
            android:name="com.thinkland.juheapi.openid"  
            android:value="application openid" />  

 

The initialization method needs to be called once before use

CommonFun.initialize(getApplicationContext());  

 

Import related packages

impimport com.thinkland.juheapi.common.CommonFun;import   
com.thinkland.juheapi.common.JsonCallBack;import   
com.thinkland.juheapi.data.weather.WeatherData  

 

call method

//Query the weather according to the city name/id  
        WeatherData weatherData = WeatherData.getInstance();  
        weatherData.getByCitys("苏州", 1, new JsonCallBack() {  
  
            @Override  
            public void jsonLoaded(JSONObject arg0) {  
                // TODO Auto-generated method stub  
                try {  
                    int code = arg0.getInt("resultcode");  
                    if (code == 200) {  
                        JSONObject resultJson = arg0.getJSONObject("result");  
                    }  
                } catch (JSONException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace ();  
                }  
            }  
        });  

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326554033&siteId=291194637