android 天气预报

获取城市天气数据的途径有很多,这里主要使用高德实现。

1、首先到高德官网:http://lbs.amap.com/dev/key/app 注册一个开发者账号,然后在应用管理-->我的应用那里添加一个应用,

android 天气预报_第1张图片

2、获取应用签名,可以打开as的build-->generate Signed APK...-->Create new ...>填写完后点ok-->一直下一步到完成就好。这样会生成一个.jsk文件。

关于获取SHAI上面有文档可以查看http://lbs.amap.com/faq/top/hot-questions/249 但是其中要注意的是,如果在as的Terminal那里获取的话要先进入jdk的bin目录,因为keytool在此目录下,然后再执行keytool-list -v –keystore 签名文件路径,然后在输入密码就可以得到SHA1在拷贝过去便可。

android 天气预报_第2张图片

3、当将这些都填完提交就可以得到一个key,

android 天气预报_第3张图片

然后加到权限文件中,并添加网络权限




    

    
        
            
                

                
            
        


        
    

4、到高德官网http://lbs.amap.com/api/android-sdk/download 下载jar包然后添加到libs下,在build.gradle加上下面代码
compile files('libs/AMap3DMap_5.2.1_AMapSearch_5.2.1_20170630.jar')
5、在代码中也可以获取SHAI,可以检查自己获取的是否正确,也可以直接这样获取SHAI

 public String sHA1(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
            }
            String result = hexString.toString();
            return result.substring(0, result.length()-1);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
最后就可以通过城市获取到该城市天气了

package com.example.a550211.wether;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLive;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearch;
import com.amap.api.services.weather.WeatherSearchQuery;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements WeatherSearch.OnWeatherSearchListener {
    final String tag = MainActivity.class.getSimpleName();
    final String cityString = "武汉";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WeatherSearchQuery weatherQuery = new WeatherSearchQuery(cityString, WeatherSearchQuery.WEATHER_TYPE_LIVE);
        WeatherSearch weatherSearch = new WeatherSearch(MainActivity.this);
        weatherSearch.setQuery(weatherQuery);
        weatherSearch.setOnWeatherSearchListener(MainActivity.this);
        weatherSearch.searchWeatherAsyn();


        Log.e("MainActivity", "==="+sHA1(getApplication()));
    }

    @Override
    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {
        if (i == 1000) {
            LocalWeatherLive liveWeather = localWeatherLiveResult.getLiveResult();
            Log.e("MainActivity",liveWeather.getReportTime()+"发布");
            Log.e("MainActivity",liveWeather.getWeather()+" ="+liveWeather.getTemperature()+"°"+liveWeather.getWindDirection()
                    +"风     "+liveWeather.getWindPower()+"级"+" 湿度         "+liveWeather.getHumidity()+"%");
        } else {
            Log.e("", "查询天气失败");
        }

    }

    @Override
    public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {

    }

    public String sHA1(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
            }
            String result = hexString.toString();
            return result.substring(0, result.length()-1);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
android 天气预报_第4张图片







你可能感兴趣的:(android,android,weather,天气,天气预报,高德)