使用高德天气api写一个天气andorid的app
时间: 2024-04-27 08:24:24 浏览: 115
自驾游导航APP.doc
这里提供一个简单的使用高德天气API编写天气Android应用的步骤:
1. 注册高德开发者账号,创建应用并开通天气API服务。
2. 在Android Studio中创建一个新项目,并在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.INTERNET"/>
```
3. 在build.gradle文件的dependencies中添加以下依赖:
```
implementation 'com.amap.api:3dmap:7.7.0'
implementation 'com.amap.api:location:5.0.0'
implementation 'com.amap.api:search:7.7.0'
```
4. 在MainActivity中添加以下代码,获取当前定位信息:
```java
public class MainActivity extends AppCompatActivity implements AMapLocationListener {
private AMapLocationClient locationClient;
private TextView tvCity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvCity = findViewById(R.id.tv_city);
//初始化定位
locationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听
locationClient.setLocationListener(this);
//启动定位
locationClient.startLocation();
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
//定位成功回调信息,设置相关消息
tvCity.setText(aMapLocation.getCity());
} else {
//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo());
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//销毁定位客户端,同时销毁本地定位服务。
locationClient.onDestroy();
}
}
```
5. 在高德开放平台中获取天气信息,可以使用以下代码:
```java
public class WeatherUtil {
private static final String URL = "https://restapi.amap.com/v3/weather/weatherInfo";
private static final String KEY = "YOUR_KEY"; //替换成你的高德开放平台应用Key
public static void getWeatherInfo(String city, Callback<WeatherResult> callback) {
OkHttpClient client = new OkHttpClient.Builder().build();
HttpUrl httpUrl = HttpUrl.parse(URL).newBuilder()
.addQueryParameter("key", KEY)
.addQueryParameter("city", city)
.addQueryParameter("extensions", "base")
.addQueryParameter("output", "json")
.build();
Request request = new Request.Builder()
.url(httpUrl)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
}
}
```
6. 在MainActivity中调用getWeatherInfo方法获取天气信息,并更新UI:
```java
public class MainActivity extends AppCompatActivity implements AMapLocationListener {
private AMapLocationClient locationClient;
private TextView tvCity;
private TextView tvWeather;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvCity = findViewById(R.id.tv_city);
tvWeather = findViewById(R.id.tv_weather);
//初始化定位
locationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听
locationClient.setLocationListener(this);
//启动定位
locationClient.startLocation();
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
//定位成功回调信息,设置相关消息
tvCity.setText(aMapLocation.getCity());
WeatherUtil.getWeatherInfo(aMapLocation.getCity(), new Callback<WeatherResult>() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
WeatherResult result = new Gson().fromJson(json, WeatherResult.class);
runOnUiThread(() -> tvWeather.setText(result.getWeather()));
}
});
} else {
//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo());
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//销毁定位客户端,同时销毁本地定位服务。
locationClient.onDestroy();
}
}
```
这样,我们就可以根据当前定位获取到城市,然后调用高德天气API获取天气信息,并将结果展示在UI上了。当然,还有很多细节需要处理,比如异常处理、UI美化等等,这里只是提供一个简单的示例供参考。
阅读全文