Retrofit2极简单使用(入门)
程序员文章站
2024-02-29 23:48:40
...
android的网络请求框架不少,如HttpUrlConnection、HttpClient、AsyncHttpClient、Volley、okHttp和Retrofit等等。
今天就说一下Retrofit的简单使用,其实Retrofit的底层也是使用okHttp的,就是把okHttp封装的一下,具体的的请求还是
交给okHttp来执行.
不多说了,下面就说一下具体的使用
使用之前肯定要加载依赖库
compile 'com.squareup.retrofit2:retrofit:2.1.0'
之后开始具体代码
这次demo的请求数据用天气预报的数据https://www.sojson.com/open/api/weather/json.shtml?city=北京
开始写之前记得把网络权限打开
新建activity:RetrofitTestActivity
public class RetrofitTestActivity extends AppCompatActivity {
private Button btn;
private EditText editText;
private TextView textView;
//必须以 /(斜线) 结束,不然会抛出一个IllegalArgumentException异常
private String url="https://www.sojson.com/open/api/weather/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit_test);
btn =(Button) findViewById(R.id.btn);
editText = (EditText) findViewById(R.id.editR);
textView = (TextView) findViewById(R.id.text);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String city = editText.getText().toString();
//创建Retrofit对象 设定网络请求的路径
//GsonConverterFactory 是Retrofit解析成Gson格式
Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).build();
//获取请求对象
RetrofitTestIm retrofitTestIm = retrofit.create(RetrofitTestIm.class);
//获取请求的方法 city为参数
Call<WeatherBen> call = retrofitTestIm.getweather(city);
//开始请求可以是同步 也可以是异步
call.enqueue(new Callback<WeatherBen>() {
@Override
public void onResponse(Call<WeatherBen> call, Response<WeatherBen> response) {
//获得数据
WeatherBen.DataEntity.ForecastEntity list = response.body().getData().getForecast().get(0);
String wearther = "date:"+list.getDate()+
"\n sunrise:"+list.getSunrise()+
"\n high:"+list.getHigh()+
"\n low:"+list.getLow()+
"\n sunset:"+list.getSunset()+
"\n aqi:"+list.getAqi()+
"\n fx:"+list.getFx()+
"\n fl:"+list.getFl()+
"\n type:"+list.getType()+
"\n notice:"+list.getNotice();
textView.setText(wearther);
}
@Override
public void onFailure(Call<WeatherBen> call, Throwable t) {
//获取失败
}
});
}
});
}
}
在此之前需建立请求对象,其实Retrofit就是创建一个java接口来描述http请求
接口RetrofitTestIm:
public interface RetrofitTestIm {
@GET("json.shtml")
Call<WeatherBen> getweather(@Query("city") String city);
}
http的请求方法有很多种:GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS和HTTP,但是HTTP可以替换前7个。实体类WeatherBen用于存放获取的数据
手写闲麻烦的话,可以下载一个自动生成实体类插件GsonFormat,
如何使用:android studio如何使用安转使用GsonFormat
public class WeatherBen {
/**
* date : 20180425
* data : {"shidu":"41%","yesterday":{"date":"24日星期二","sunrise":"05:26","high":"高温 23.0℃","fx":"北风","low":"低温 10.0℃","fl":"3-4级","sunset":"19:01","aqi":38,"type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},"pm25":23,"pm10":55,"ganmao":"极少数敏感人群应减少户外活动","forecast":[{"date":"25日星期三","sunrise":"05:25","high":"高温 25.0℃","fx":"西南风","low":"低温 11.0℃","fl":"3-4级","sunset":"19:02","aqi":63,"type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"26日星期四","sunrise":"05:23","high":"高温 26.0℃","fx":"西南风","low":"低温 13.0℃","fl":"<3级","sunset":"19:03","aqi":69,"type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"27日星期五","sunrise":"05:22","high":"高温 25.0℃","fx":"西南风","low":"低温 14.0℃","fl":"<3级","sunset":"19:04","aqi":64,"type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"28日星期六","sunrise":"05:21","high":"高温 27.0℃","fx":"西南风","low":"低温 17.0℃","fl":"3-4级","sunset":"19:05","aqi":85,"type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"29日星期日","sunrise":"05:19","high":"高温 28.0℃","fx":"东南风","low":"低温 16.0℃","fl":"<3级","sunset":"19:06","aqi":96,"type":"多云","notice":"阴晴之间,谨防紫外线侵扰"}],"wendu":"16","quality":"良"}
* city : 北京
* count : 1000
* message : Success !
* status : 200
*/
private String date;
private DataEntity data;
private String city;
private int count;
private String message;
private int status;
public void setDate(String date) {
this.date = date;
}
public void setData(DataEntity data) {
this.data = data;
}
public void setCity(String city) {
this.city = city;
}
public void setCount(int count) {
this.count = count;
}
public void setMessage(String message) {
this.message = message;
}
public void setStatus(int status) {
this.status = status;
}
public String getDate() {
return date;
}
public DataEntity getData() {
return data;
}
public String getCity() {
return city;
}
public int getCount() {
return count;
}
public String getMessage() {
return message;
}
public int getStatus() {
return status;
}
public static class DataEntity {
/**
* shidu : 41%
* yesterday : {"date":"24日星期二","sunrise":"05:26","high":"高温 23.0℃","fx":"北风","low":"低温 10.0℃","fl":"3-4级","sunset":"19:01","aqi":38,"type":"多云","notice":"阴晴之间,谨防紫外线侵扰"}
* pm25 : 23.0
* pm10 : 55.0
* ganmao : 极少数敏感人群应减少户外活动
* forecast : [{"date":"25日星期三","sunrise":"05:25","high":"高温 25.0℃","fx":"西南风","low":"低温 11.0℃","fl":"3-4级","sunset":"19:02","aqi":63,"type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"26日星期四","sunrise":"05:23","high":"高温 26.0℃","fx":"西南风","low":"低温 13.0℃","fl":"<3级","sunset":"19:03","aqi":69,"type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"27日星期五","sunrise":"05:22","high":"高温 25.0℃","fx":"西南风","low":"低温 14.0℃","fl":"<3级","sunset":"19:04","aqi":64,"type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"28日星期六","sunrise":"05:21","high":"高温 27.0℃","fx":"西南风","low":"低温 17.0℃","fl":"3-4级","sunset":"19:05","aqi":85,"type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"29日星期日","sunrise":"05:19","high":"高温 28.0℃","fx":"东南风","low":"低温 16.0℃","fl":"<3级","sunset":"19:06","aqi":96,"type":"多云","notice":"阴晴之间,谨防紫外线侵扰"}]
* wendu : 16
* quality : 良
*/
private String shidu;
private YesterdayEntity yesterday;
private double pm25;
private double pm10;
private String ganmao;
private List<ForecastEntity> forecast;
private String wendu;
private String quality;
public void setShidu(String shidu) {
this.shidu = shidu;
}
public void setYesterday(YesterdayEntity yesterday) {
this.yesterday = yesterday;
}
public void setPm25(double pm25) {
this.pm25 = pm25;
}
public void setPm10(double pm10) {
this.pm10 = pm10;
}
public void setGanmao(String ganmao) {
this.ganmao = ganmao;
}
public void setForecast(List<ForecastEntity> forecast) {
this.forecast = forecast;
}
public void setWendu(String wendu) {
this.wendu = wendu;
}
public void setQuality(String quality) {
this.quality = quality;
}
public String getShidu() {
return shidu;
}
public YesterdayEntity getYesterday() {
return yesterday;
}
public double getPm25() {
return pm25;
}
public double getPm10() {
return pm10;
}
public String getGanmao() {
return ganmao;
}
public List<ForecastEntity> getForecast() {
return forecast;
}
public String getWendu() {
return wendu;
}
public String getQuality() {
return quality;
}
public static class YesterdayEntity {
/**
* date : 24日星期二
* sunrise : 05:26
* high : 高温 23.0℃
* fx : 北风
* low : 低温 10.0℃
* fl : 3-4级
* sunset : 19:01
* aqi : 38.0
* type : 多云
* notice : 阴晴之间,谨防紫外线侵扰
*/
private String date;
private String sunrise;
private String high;
private String fx;
private String low;
private String fl;
private String sunset;
private double aqi;
private String type;
private String notice;
public void setDate(String date) {
this.date = date;
}
public void setSunrise(String sunrise) {
this.sunrise = sunrise;
}
public void setHigh(String high) {
this.high = high;
}
public void setFx(String fx) {
this.fx = fx;
}
public void setLow(String low) {
this.low = low;
}
public void setFl(String fl) {
this.fl = fl;
}
public void setSunset(String sunset) {
this.sunset = sunset;
}
public void setAqi(double aqi) {
this.aqi = aqi;
}
public void setType(String type) {
this.type = type;
}
public void setNotice(String notice) {
this.notice = notice;
}
public String getDate() {
return date;
}
public String getSunrise() {
return sunrise;
}
public String getHigh() {
return high;
}
public String getFx() {
return fx;
}
public String getLow() {
return low;
}
public String getFl() {
return fl;
}
public String getSunset() {
return sunset;
}
public double getAqi() {
return aqi;
}
public String getType() {
return type;
}
public String getNotice() {
return notice;
}
}
public static class ForecastEntity {
/**
* date : 25日星期三
* sunrise : 05:25
* high : 高温 25.0℃
* fx : 西南风
* low : 低温 11.0℃
* fl : 3-4级
* sunset : 19:02
* aqi : 63.0
* type : 晴
* notice : 愿你拥有比阳光明媚的心情
*/
private String date;
private String sunrise;
private String high;
private String fx;
private String low;
private String fl;
private String sunset;
private double aqi;
private String type;
private String notice;
public void setDate(String date) {
this.date = date;
}
public void setSunrise(String sunrise) {
this.sunrise = sunrise;
}
public void setHigh(String high) {
this.high = high;
}
public void setFx(String fx) {
this.fx = fx;
}
public void setLow(String low) {
this.low = low;
}
public void setFl(String fl) {
this.fl = fl;
}
public void setSunset(String sunset) {
this.sunset = sunset;
}
public void setAqi(double aqi) {
this.aqi = aqi;
}
public void setType(String type) {
this.type = type;
}
public void setNotice(String notice) {
this.notice = notice;
}
public String getDate() {
return date;
}
public String getSunrise() {
return sunrise;
}
public String getHigh() {
return high;
}
public String getFx() {
return fx;
}
public String getLow() {
return low;
}
public String getFl() {
return fl;
}
public String getSunset() {
return sunset;
}
public double getAqi() {
return aqi;
}
public String getType() {
return type;
}
public String getNotice() {
return notice;
}
}
}
}
示例结果
版权声明:本文为博主原创文章,如需转载必须注明转载地址。https://blog.csdn.net/yedekuqi4712/article/details/80083429
推荐阅读
-
Retrofit2极简单使用(入门)
-
iBatis入门--简单使用
-
.net core api +swagger(一个简单的入门demo 使用codefirst+mysql)
-
Java入门之使用记事本写Java程序及简单命令符
-
使用C语言扩展Python程序的简单入门指引
-
MongoDB最简单的入门教程之四:使用Spring Boot操作MongoDB
-
MongoDB最简单的入门教程之二 使用nodejs访问MongoDB
-
MongoDB最简单的入门教程之三 使用Java代码往MongoDB里插入数据
-
matplotlib库的简单使用(入门)
-
PHP中使用XML-RPC构造Web Service简单入门