移步Retrofit--网络通讯框架
使用前准备
- 免费的公共接口:查询天气
https://www.apiopen.top/weatherApi?city=临沂
依赖框架
/** 添加网络框架Retrofit*/
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation "com.google.code.gson:gson:2.8.0"
接口返回数据的内容,并做Gson类解析
- 返回内容
{
"code":200,
"msg":"成功!",
"data":{
"yesterday":{
"date":"3日星期日",
"high":"高温 10℃",
"fx":"北风",
"low":"低温 1℃",
"fl":"<![CDATA[<3级]]>",
"type":"小雨"
},
"city":"临沂",
"aqi":null,
"forecast":[
{
"date":"4日星期一",
"high":"高温 9℃",
"fengli":"<![CDATA[<3级]]>",
"low":"低温 0℃",
"fengxiang":"东南风",
"type":"阴"
},
{
"date":"5日星期二",
"high":"高温 14℃",
"fengli":"<![CDATA[<3级]]>",
"low":"低温 1℃",
"fengxiang":"北风",
"type":"阴"
},
{
"date":"6日星期三",
"high":"高温 12℃",
"fengli":"<![CDATA[3-4级]]>",
"low":"低温 3℃",
"fengxiang":"东北风",
"type":"晴"
},
{
"date":"7日星期四",
"high":"高温 11℃",
"fengli":"<![CDATA[<3级]]>",
"low":"低温 0℃",
"fengxiang":"东南风",
"type":"晴"
},
{
"date":"8日星期五",
"high":"高温 11℃",
"fengli":"<![CDATA[<3级]]>",
"low":"低温 0℃",
"fengxiang":"东北风",
"type":"阴"
}
],
"ganmao":"天凉,昼夜温差较大,较易发生感冒,请适当增减衣服,体质较弱的朋友请注意适当防护。",
"wendu":"8"
}
}
- Gson数据解析
public class WeatherInfoModel {
public String code;
@SerializedName("message")
public String message;
@SerializedName("data")
public WeatherInfo weatherInfo;
public class WeatherInfo{
@SerializedName("yesterday")
public DayWeatherInfo yesterdayWeatherInfo;
public String city;
public String aqi;
@SerializedName("forecast")
public List<DayWeatherInfo> forecastWeatherInfo;
@SerializedName("ganmao")
public String hint;
public String wendu;
}
public class DayWeatherInfo{
public String date;
public String high;
public String fx;
public String low;
public String fl;
public String type;
}
@Override
public String toString(){
return "城市: " + weatherInfo.city+", 天气:"+weatherInfo.forecastWeatherInfo.get(0).type+", "+ weatherInfo.forecastWeatherInfo.get(0).high+", "
+ weatherInfo.forecastWeatherInfo.get(0).low+", 提醒:"+weatherInfo.hint;
}
Gson的分析移步Json数据解析-Gson库
创建请求接口
public interface HttpService {
/**
* 动态指定查询条件
* @Query("字段")
* */
@GET("weatherApi")
Call<WeatherInfoModel> getWeatherInfo(@Query("city") String city);
}
创建Retrofit并实例化HttpService
Retrofit retrofit = new Retrofit.Builder().
baseUrl(URL).
addConverterFactory(GsonConverterFactory.create()).
build();
mHttpService = retrofit.create(HttpService.class);
通过接口获取Call并做网络请求
Call<WeatherInfoModel> call = mHttpService.getWeatherInfo("临沂");
call.enqueue(new Callback<WeatherInfoModel>() {
@Override
public void onResponse(Call<WeatherInfoModel> call, Response<WeatherInfoModel> response) {
if (response.body().code.equals("200")){
LogShowUtil.addLog("Retrofit",response.body().toString(),true);
}else {
LogShowUtil.addLog("Retrofit", "请求失败: " + response.body().message,true);
}
}
@Override
public void onFailure(Call<WeatherInfoModel> call, Throwable t) {
}
});
注解分类
- 请求方法注解
GET、POST、PUT、DELETE、HEAD、PATCH、OPTIONS、HTTP
对应HTTP的七种请求方法,HTTP可替换前面的七种 - 标记类注解
ForMUrlEncoded、Multipart、Streaming
- @ForMUrlEncoded 注解来标明这是一个表单请求
- @Streaming 代表响应的数据以流的形式返回,如果不使用他则默认会把全部数据加载到内存,所以下周大文件时需要加上这个注解
- @Multipart 上传文件
- 参数类注解
Header、Headers、Body、Path、Field、FieldMap、Part、PartMap、Query、QueryMap等
- @Path 动态的配置URL地址
- @Query 动态指定查询条件
- @QueryMap 动态指定查询条件组
- @Field 传输数据类型键值对
- @Body 传输数据类型JSON字符串
- @Part 单个文件上传
- @PartMap 多个文件上传
- @Header 消息报头
实例
@GET("weatherApi")
Call<WeatherInfoModel> getWeatherInfo(@Query("city") String city);
GET请求
动态指定查询条件
@GET("weatherApi")
Call<WeatherInfoModel> getWeatherInfo(@QueryMap Map<String,String> params);
GET请求
动态指定查询条件组
@GET("{path}")
Call<WeatherInfoModel> getWeatherInfo(@Path ("path") String path, @Query("city") String city);
GET请求
动态的配置URL地址
动态指定查询条件
@FormUrlEncoded
@POST("weatherApi")
Call<WeatherInfoModel> postWeatherInfo(@Field("city") String city);
POST请求
传输数据类型为键值对
注解来标明这是一个表单请求
@POST("weatherApi")
Call<WeatherInfoModel> postWeatherInfo(@Body CityBody body);
POST请求
传输数据类型为JSON字符串
网友评论