Retrofit:Android和Java的类型安全的HTTP客户端。
Introduction:
1.Retrofit把你的HTTP API转换成Java接口
public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
2.Retrofit类去实现GitHubService的接口
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
GitHubService service = retrofit.create(GitHubService.class);
3.每个Call去创建GitHubService可以实现同步或者异步去请求远程网络服务器
Call<List<Repo>> repos = service.listRepos("octocat");
使用注解去描述HTTP请求:
1.支持URL参数替换和查询参数
2.对象转换为请求主体(例如,JSON,协议缓冲区)
3.Multipart请求主体和文件上传
API Declaration:
在接口方法及其参数注释指示请求将如何处理。
1.REQUEST METHOD(请求方法):
每个方法都必须有一个HTTP注释提供的请求方法和相对URL。有五个内置的注释:GET, POST, PUT, DELETE, and HEAD。资源的相对URL在注释中指定。
例如:
@GET("users/list")
还可以指定URL的查询参数,例如:
@GET("users/list?sort=desc")
2.URL MANIPULATION(URL操作):
请求的URL可以动态使用的方法替换块和参数进行更新。替换使用{},里面是字母数字字符串,相应的参数必须与@path注释使用相同的字符串。
例如:
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId);
查询参数也可以被添加,例如:
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort);
对于复杂的查询参数组合可以使用map,例如:
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options);
3.REQUEST BODY(请求体):
一个对象可以作为一个HTTP请求正文使用的@Body注解指定,例如:
@POST("users/new")
Call<User> createUser(@Body User user);
该对象也将被使用在retrofit实例指定一个转换器转换。如果没有添加转换器,仅仅RequestBody可以被使用。
4.FORM ENCODED AND MULTIPART(表单和多表单):
方法也可以声明发送form-encoded 和multipart data。
form-encoded数据发送当@FormUrlEncoded方法存在时,每一个key-value使用注解@Field包含名称和提供该值的对象,例如:
@FormUrlEncoded
@POST("user/edit")
Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last);
多请求被使用当@Multipart存在方法上时,声明部分使用@Part注解,例如:
@Multipart
@PUT("user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);
Multipart parts使用一个Retrofit的转换器或者他们可以实现RequestBody来处理自己的序列化。
5.HEADER MANIPULATION(处理请求头):
你可以设置使用@Headers注释静态头的方法,例如:
@Headers("Cache-Control: max-age=640000")
@GET("widget/list")
Call<List<Widget>> widgetList();
多个请求头,例如:
@Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit-Sample-App"
})
@GET("users/{username}")
Call<User> getUser(@Path("username") String username);
注意头不会互相覆盖。具有相同名称的所有头将被包括在该请求。
一个请求头使用@Header注释可以动态更新。@Header必须提供相应的参数。如果该值为null,头就会被忽略掉。否则,toString将被回调这个value,使用其结果,例如:
@GET("user")
Call<User> getUser(@Header("Authorization") String authorization)
需要被添加到每请求头可以用一个OkHttp拦截来指定。
6.SYNCHRONOUS VS. ASYNCHRONOUS(同步VS异步):
call实例可以被同步或异步执行,每一个实例只能使用一次,但是使用clone()方法将创建新的实例被使用。
在Android中,回调函数将在主线程上执行,在JVM虚拟机上,回调函数会发生在同一线程执行HTTP请求。
Retrofit Configuration(配置)
Retrofit是API接口的类转化为可调用的对象。retorfit将为你的平台提供健全的默认值但是它允许定制。
CONVERTERS(转换器):
默认情况下,retrofit只能反序列化okhttp中的ResponseBody类型和它只能接受其@Body RequestBody类型。
转换器可以添加到其他类型的支持,以下是6个常用的序列化库:
转换器可以添加到其他类型的支持,以下是6个常用的序列化库:
Gson: com.squareup.retrofit2:converter-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
Retrofit的基本配置项目中的使用
Android studio中的配置Retrofit
在
gradle添加依赖
:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Retrofit需要至少Java 7或Android 2.3。
一个简单使用Retrofit中的例子
1.首先聚合上申请的接口,身份证查询:
地址:http://apis.juhe.cn/idcard/index?key=您申请的KEY&cardno=330326198903081211
2.这里根据输入身份证号获取个人的基本信息,接下来创建接口:
public interface UserCardnoService {
//基本信息查询
@GET("/idcard/index")
Call<InforBean> getInfoResult(@Query("key") String key, @Query("cardno") String cardno);
//信息是否泄漏
@GET("/idcard/leak")
Call<LeakBean> getLeakResult(@Query("key") String key, @Query("cardno") String cardno);
//身份证是否挂失
@GET("/idcard/loss")
Call<LossBean> getLossResult(@Query("key") String key, @Query("cardno") String cardno);
}
3.创建实体类,这里放的实体类是基本信息的查询:
public class InforBean {
/**
* resultcode : 200
* reason : 成功的返回
* result : {"area":"浙江省温州市平阳县","sex":"男","birthday":"1989年03月08日"}
*/
public String resultcode;
public String reason;
public int error_code;
/**
* area : 浙江省温州市平阳县
* sex : 男
* birthday : 1989年03月08日
*/
public ResultBean result;
public static class ResultBean {
public String area;
public String sex;
public String birthday;
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
public String getResultcode() {
return resultcode;
}
public void setResultcode(String resultcode) {
this.resultcode = resultcode;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
}
4.创建用户信息查询的类UserInfoActivity,构建retrofit实例访问接口,解析数据,对数据进行赋值:
public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener {
public static final String TAG = "UserInfoActivity";
private EditText mEtInput;
private Button btnQuery;
private TextView mTvArea;
private TextView mTvSex;
private TextView mTvBirthday;
private LinearLayout llInfo;
private String mCardno;
private Context mContext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = UserInfoActivity.this;
setContentView(R.layout.base_layout);
initView();
}
private void initView() {
mEtInput = (EditText) findViewById(R.id.etInput);
btnQuery = (Button) findViewById(R.id.btnQuery);
mTvArea = (TextView) findViewById(R.id.tvArea);
mTvSex = (TextView) findViewById(R.id.tvSex);
mTvBirthday = (TextView) findViewById(R.id.tvBirthday);
llInfo = (LinearLayout) findViewById(R.id.ll_Info);
btnQuery.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mCardno = mEtInput.getText().toString().trim();
if (!mCardno.matches(RegexpUtils.USER_CARD)) {
Toast.makeText(mContext, "输入有误,请重新输入!", Toast.LENGTH_SHORT).show();
} else {
getNetData();
}
}
private void getNetData() {
//构建rtrofit实例
Retrofit retrofit = new Retrofit
.Builder()
.baseUrl(GlobalContact.BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
UserCardnoService infoService = retrofit.create(UserCardnoService.class);
Call<InforBean> call = infoService.getInfoResult(GlobalContact.KEY, mCardno);
call.enqueue(new Callback<InforBean>() {
@Override
public void onResponse(Call<InforBean> call, Response<InforBean> response) {
Log.i(TAG, "onResponse: " + response.body().toString());
if (response.isSuccessful()) {
llInfo.setVisibility(View.VISIBLE);
//对数据进行赋值
mTvArea.setText(response.body().result.area);
mTvSex.setText(response.body().result.sex);
mTvBirthday.setText(response.body().result.birthday);
} else {
llInfo.setVisibility(View.GONE);
}
}
@Override
public void onFailure(Call<InforBean> call, Throwable t) {
Log.i(TAG, "onFailure: " + t.toString());
}
});
}
}