最近写了一个天气的Demo,调用开源Weather API:
【 完整的项目地址】
https://api.openweathermap.org/
在网络请求部分使用了Retrofit,这里学习了解一哈,并做个记录。
一、Retrofit2.0介绍:
原理的话如图:
Retorfit其实就是一个注解型的网络接口封装,实际调用还是通过OkHttp来实现,只是对他的一层封装。
然后它有很多注解类型:GET、POST、PATH等,我这里只是调用了GET,然后参数通过Query来进行处理。
二、Retorfit具体使用:
1.权限添加:
<uses-permission android:name="android.permission.INTERNET"/>
2.gradle依赖:
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
3.创建回调的数据解析类:
public class WeatherModel implements IWeatherModel {
private IWeatherPresenter mIWeatherPresenter;
private RetrofitInterface mRetrofitInterface;
public WeatherModel(IWeatherPresenter IWeatherPresenter) {
mIWeatherPresenter = IWeatherPresenter;
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Constants.BASE_URL)
.build();
mRetrofitInterface = retrofit.create(RetrofitInterface.class);
}
@Override
public void getCurrentWeather(final String city) {
Call<WeatherCurrent> current = mRetrofitInterface.getWeatherCurrent(city, "metric",
Constants.API_KEY, "zh_cn");
current.enqueue(new Callback<WeatherCurrent>() {
@Override
public void onResponse(Call<WeatherCurrent> call, Response<WeatherCurrent> response) {
if (response.isSuccessful()) {
mIWeatherPresenter.onCurrentWeather(response.body());
} else {
mIWeatherPresenter.onCurrentFail(city);
}
}
@Override
public void onFailure(Call<WeatherCurrent> call, Throwable t) {
mIWeatherPresenter.onCurrentFail(t.getMessage());
}
});
}
@Override
public void getForecastWeather(final String city) {
Call<WeatherForecast> forecast = mRetrofitInterface.getWeatherForecast(city, "metric", 20,
Constants.API_KEY);
forecast.enqueue(new Callback<WeatherForecast>() {
@Override
public void onResponse(Call<WeatherForecast> call, Response<WeatherForecast> response) {
if (response.isSuccessful()) {
mIWeatherPresenter.onForecastWeather(response.body());
} else {
mIWeatherPresenter.onForecastFail(city);
}
}
@Override
public void onFailure(Call<WeatherForecast> call, Throwable t) {
mIWeatherPresenter.onForecastFail(t.getMessage());
}
});
}
}
4.创建网络请求的接口:
public interface RetrofitInterface {
@GET("weather")
Call<WeatherCurrent> getWeatherCurrent(@Query("q") String cityName,
@Query("units") String units,
@Query("APPID") String ApiId,
@Query("lang") String lang);
@GET("forecast")
Call<WeatherForecast> getWeatherForecast(@Query("q") String cityName,
@Query("units") String units,
@Query("cnt") int count,
@Query("APPID") String ApiId);
}
5.定义的接口:
public interface IWeatherModel {
void getCurrentWeather(String city);
void getForecastWeather(String city);
}
6.常用的参数类:
public class Constants {
public static final String API_KEY = "343cb181f9cfa66aa05e5caab8c30cfa";
public static final String BASE_URL = "https://api.openweathermap.org/data/2.5/";
public static final String ICON_URL = "http://openweathermap.org/img/w/";
String key1 = "7e5dcacfa46a733e084fd65210c78e96";
String key2 = "343cb181f9cfa66aa05e5caab8c30cfa";
private static final boolean DEBUG = true;
public static void debug(String str){
if (DEBUG)
Log.d("fengjw", str);
}
}
如此便完成了最简单的Retorfit使用。
【 完整的项目地址】
参考文档:
https://blog.csdn.net/carson_ho/article/details/73732076