使用步骤:
步骤1:定义接口
public interface GitHubService {
/*
*Post多参数Map
*Post方法 "getnews" 为路径
* mapPost方法名
* @FieldMap多个请求参数
* newsPojo 接受的实体类
* @FormUrlEncoded将会自动将请求参数的类型调整为application/x-www-form-urlencoded 不能用于Get请求
*/
@FormUrlEncoded
@POST("getnews")
Call<newsPojo> mapPost(@FieldMap Map<String,Object> map);
/*
*Post多参数
*Post方法 "getnews" 为路径
* singlePost方法名
* @Field多个请求参数
* newsPojo 接受的实体类
* @FormUrlEncoded将会自动将请求参数的类型调整为application/x-www-form-urlencoded 不能用于Get请求
*/
@FormUrlEncoded
@POST("getnews")
Call<newsPojo> singlePost(@Field("appey") String appkey,@Field("lasttime") String lasttime);
/*
*Post 实体类参数
*所有参数统一封装到类中
*/
@FormUrlEncoded
@POST("getnews")
Call<newsPojo> bodyPost(@Body newsPaper news);
/*
*单参数Get方法
*/
@GET("getnews")
Call<newsPojo> singleGet(@Query("appkey") String appkey,@Query("appkey1") String appkey1);
/*
*多参数Get方法
*/
@GET("getnews")
Call<newsPojo> mapGet(@QueryMap Map<String, String> options);
/*
*List集合参数Get方法
*/
@GET("getnews")
Call<newsPojo> ListGet(@Query("user")List<String> list);
/*
*相对路径参数地址 @Path
* List<newsPaper> 接受的集合
* @Path可以用于任何请求方式,例如Post、Put等。
*/
@GET("group/{id}/users")
Call<List<newsPaper>> groupList(@Path("id") int groupId, @Query("sort") String sort);
/*
**单个文件上传
*/
@Multipart
@PUT("upload") // @POST("upload")
Call<ResponseBody> updateFile(@Part("description") RequestBody description ,@Part MultipartBody.Part file);
@Multipart
@POST("upload")
Call<ResponseBody> uploadMultipleFiles(@Part("description") RequestBody description,
@Part MultipartBody.Part file1,
@Part MultipartBody.Part file2);
/*
*添加自定义的header
* 静态方法
*/
@Headers("Cache-Control: max-age=640000")
@GET("getnews")
Call<newsPojo> headerGet(@QueryMap Map<String, String> options);
/*
*静态 多个定义
*/
@Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit-Sample-App"
})
@GET("users/{username}")
Call<newsPaper> getUser(@Path("username") String username);
/*
*动态方法
*/
@GET("getnews")
Call<newsPojo> headNews(@Header("Content-Range") String contentRange, @Query("q") String name);
/*
*Post提交Json参数
* RequestBody 为Json、等数据
*/
@Headers({"Content-Type: application/json","Accept: application/json"})//需要添加头
@POST("user")
Call<newsPaper> postJson(@Body RequestBody json);//传入的参数为RequestBody
}
调用:
//步骤1
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.80.2:8080")
.addConverterFactory(GsonConverterFactory.create()) //数据转换方式 GsonConverterFactory.create() Gson解析
.build();
//步骤2
GitHubService service = retrofit.create(GitHubService.class);
//步骤3 调用请求方法,并得到Call实例
Call<newsPojo> call = service.singlePost("appkey","0");
//步骤4 同步请求
try {
newsPojo response = call.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
//步骤4 异步请求
call.enqueue(new Callback<newsPojo>() {
@Override
public void onResponse(Call<newsPojo> call, Response<newsPojo> response) {
//请求成功返回的结果
System.out.println("返回数据"+response.body().getReason());
}
@Override
public void onFailure(Call<newsPojo> call, Throwable t) {
}
});
//步骤1 Post提交Json数据
Retrofit retrofitJson = new Retrofit.Builder()
.baseUrl("http://192.168.80.2:8080/fagagagag")
.addConverterFactory(GsonConverterFactory.create())
.build();
//步骤2 Post提交Json数据
GitHubService serviceJson = retrofit.create(GitHubService.class);
//步骤3 Post提交Json数据
newsPaper pojo = new newsPaper();
Gson gson=new Gson();
String json= gson.toJson(pojo);
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
//步骤4 Post提交Json数据
Call<newsPaper> callJson = serviceJson.postJson(body);
//步骤5 请求
callJson.enqueue(new Callback<newsPaper>() {
@Override
public void onResponse(Call<newsPaper> call, Response<newsPaper> response) {
}
@Override
public void onFailure(Call<newsPaper> call, Throwable t) {
}
});
}