文章目录
1.Retrofit是什么
Retrofit主要是对Android网络请求的框架进行封装,底层基于OkHttp。
换句话说,网络请求的本质还是OkHttp完成,而Retrofit仅负责网络请求接口的封装。
主要是使用Retrofit接口封装请求参数,header头部,Url信息等,然后交给OkHttp完成后续的网络请求,当服务端返回数据时,OkHttp又将返回的原始数据交给Retrofit,Retrofit再根据需求解析数据。
简单明了的讲,Retrofit主要是对网络请求进行了封装。
2.Retrofit的使用
注:以下以访问词霸api为例,URL为“http://fy.iciba.com/ajax.php?a=fy&f=auto&t=auto&w=hello%20world”
2.1添加依赖
- 在app包下的build.gradle中
dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.6.1'//Retrofit库
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'//GSON解析
}
- 添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
2.2创建接收服务器返回数据的类
可以使用GsonFormat生成对应的Java Bean类,倘若不了解GsonFormat可以移步这里。
//json数据
{
"status":1,
"content":{
"from":"en",
"to":"zh-CN",
"vendor":"ciba",
"out":" 你好,世界",
"ciba_use":"来自机器翻译。",
"ciba_out":"",
"err_no":0
}
}
//对应的java bean类
public class TranslationBean {
private int status;
private ContentBean content;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public ContentBean getContent() {
return content;
}
public void setContent(ContentBean content) {
this.content = content;
}
public static class ContentBean {
private String from;
private String to;
private String vendor;
private String out;
private String ciba_use;
private String ciba_out;
private int err_no;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getVendor() {
return vendor;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
public String getOut() {
return out;
}
public void setOut(String out) {
this.out = out;
}
public String getCiba_use() {
return ciba_use;
}
public void setCiba_use(String ciba_use) {
this.ciba_use = ciba_use;
}
public String getCiba_out() {
return ciba_out;
}
public void setCiba_out(String ciba_out) {
this.ciba_out = ciba_out;
}
public int getErr_no() {
return err_no;
}
public void setErr_no(int err_no) {
this.err_no = err_no;
}
}
//定义输出返回数据的方法
public void show(){
Log.e("翻译",content.out);
}
}
2.3创建用于描述网络请求的接口
public interface TransApi {
@GET("ajax.php?a=fy&f=auto&t=auto&w=hi%20world")
Call<TranslationBean> getCall();
// @GET注解的作用:采用Get方法发送网络请求
// getCall() = 接收网络请求数据的方法
// 其中返回类型为Call<*>,*是接收数据的类(即上面定义的TranslationBean类),返回这个类的对象
}
2.4创建Retrofit实例
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("http://fy.iciba.com/")//网络请求的url地址
.addConverterFactory(GsonConverterFactory.create())//数据解析器,解析json
.build();
2.5创建网络请求接口实例
//创建网络请求接口的实例
TransApi request=retrofit.create(TransApi.class);
//对发送请求进行封装
Call<TranslationBean> call=request.getCall();
2.6发送网络请求
//发送网络请求(异步)
call.enqueue(new Callback<TranslationBean>() {
//请求成功时调用
@Override
public void onResponse(Call<TranslationBean> call, Response<TranslationBean> response) {
response.body().show();
}
//请求失败时调用
@Override
public void onFailure(Call<TranslationBean> call, Throwable t) {
Log.e("MainActivity","连接失败");
}
});
//发送网络请求(同步)
try{
Response<TranslationBean> response=call.execute();
} catch (Exception e){
e.printStackTrace();
};
2.7处理返回数据
//发送网络请求(异步)
call.enqueue(new Callback<TranslationBean>() {
@Override
public void onResponse(Call<TranslationBean> call, Response<TranslationBean> response) {
response.body().show();//处理返回数据,这里是显示返回数据
}
@Override
public void onFailure(Call<TranslationBean> call, Throwable t) {
Log.e("MainActivity","连接失败");
}
});
//发送网络请求(同步)
try{
Response<TranslationBean> response=call.execute();
response.body().show();//处理返回数据,这里是显示返回数据
} catch (Exception e){
e.printStackTrace();
};