官网地址:Retrofit
GitHub地址:Retrofit
在线接口测试地址:在线接口测试
Get请求
- 编写请求接口(为什么要写成接口,以为retrofit内部执行原理是动态代理)
首先看一下请求url,很简单的一个url地址I: https://api.github.com/users/basil2style
public interface Service {
//{user}代表一个可变路径 Call<String> String代表返回类型
@GET("/users/{user}")
public Call<String> getInfo(@Path("user") String u);
}
- 主类
public class MainActivity extends AppCompatActivity implements Callback<String> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//new Retrofit.Builder().baseUrl().addConverterFactory().build()转换器不能少,不然会报错
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com").addConverterFactory(new Converter.Factory() {
@Nullable
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
//Converter转换器 第二个为转换类型
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
//返回String类型
return value.string();
}
};
}
}).build();
//得到的拼接url为: https://api.github.com/users/basil2style
Call<String> info=service.getInfo("basil2style");
info.enqueue(this);
}
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.e("info",call.request().url()+response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("info",call.request().url()+t.toString());
}
}
注意:别忘了加网络权限,还有刚开始时,我以为去掉{path},然后@Path改成@Query也可以,但实际上@Query请求的为键值对,如果请求接口如下:得到的完整url地址为:https://api.github.com/users/?user=basil2stylenull,明显不符合我们的要求
public interface Service {
@GET("/users/")
public Call<String> getInfo(@Query("user") String user);
}
- 结果用实体类接收
引入gson和retrofit
compile ‘com.squareup.retrofit2:retrofit:2.3.0’
compile ‘com.squareup.retrofit2:converter-gson:2.3.0’
- 实体类
public class Info {
//序列化 当名字不同时可以将请求的参数和本地的参数匹配
@SerializedName("login")
private String login;
@SerializedName("id")
private String id;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setLogin(String login) {
this.login = login;
}
public String getLogin() {
return login;
}
}
- 请求接口
public interface Service {
// public Call<String> getInfo(@Path("user") String user);3
@GET("/users/{user}")
public Call<Info> getInfo(@Path("user") String u);
}
- 主类
。。。。。。
//GsonCoverterFactory gson转换工厂
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com").addConverterFactory(GsonConverterFactory.create()).build();
Service service = retrofit.create(Service.class);
Call<Info> info=service.getInfo("basil2style");
//异步请求
info.enqueue(this);
}
@Override
public void onResponse(Call<Info> call, Response<Info> response) {
Log.e("info",call.request().url()+response.body().getLogin());
}
@Override
public void onFailure(Call<Info> call, Throwable t) {
Log.e("info",call.request().url()+"");
t.printStackTrace();
}
Post请求
- 请求接口稍有改变,其他内容相似
ublic interface Service {
// public Call<String> getInfo(@Path("user") String user);
@POST("/users/{user}")
//当有参数@Field或请求体@时必须声明@FormUrlEncodedBody get请求中的@query相当于post中的@Field
public Call<Info> getInfo(@Path("user") String u);
}
注意:有的接口不支持post请求