<meta charset="utf-8">
一、正常的請(qǐng)求方式
使用retrofit做解析ApiServse中
@POST("")
Observable<需要解析實(shí)體類>getList2(@Header("User-Agent") String s);//括號(hào)中請(qǐng)求頭可為空
Activity中
Observable list = getRetrofit("http://www.xxx.com/").create(ApiService.class)
.getList2("rxl");//如果上面請(qǐng)求頭為空這個(gè)括號(hào)也為空
getNetData(list);
二吁朦、動(dòng)態(tài)傳值方式
使用retrofit做解析ApiServse中
@POST("{list}/{newId}/{page}-10.html")
Observable<需要解析實(shí)體類> getList(@Header("User-Agent") String s,
@Path("list") String list,@Path("newId") String newId,@Path("page")int page);
Activity中
Observable list = getRetrofit("http://www.xxxx.com/").create(ApiService.class)
.getList("rxl","list","T134",0);
getNetData(list);
三、拼接參數(shù)提交方式
1尿这、Form表單正常的提交方式
使用retrofit做解析ApiServse中
@FormUrlEncoded
@POST("")
Observable<需要解析實(shí)體類> withParams(@Field("createCollegeId") String createCollegeId,@Field("loginName")String loginName,@Field("password")String password);
Activity中
Observable observable = getRetrofit("http://www.xxx.com/").create(ApiService.class)
.withParams("73","15100133517","a12345");
getNetData(observable);
2撞羽、map表單提交方式
使用retrofit做解析ApiServse中
@FormUrlEncoded
@POST("")
Observable<需要解析實(shí)體類> withMap(@FieldMap Map params);
Activity中
final Map params =new HashMap<>();
params.put("createCollegeId","73");
params.put("loginN","15100133517");
params.put("password","a12345");
Observable observable = getRetrofit("http://www.xxx.com/").create(ApiService.class)
.withMap(params);
getNetData(observable);
四认罩、帶請(qǐng)求體的請(qǐng)求方式(body以json形式傳入)
使用retrofit做解析ApiServse中
@POST(".")
Observable <需要解析實(shí)體類> getData(@Body RequestBody requestBody);
Activity中
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON,"{"userId":"aadc2d9fe76f4b89bf37ca738e23eafe","channelId":"b9240eee3b0211e8b64c00163e30445d","cursor": 0}");
Observable observable = getRetrofit("http://www.xxx.com/").create(ApiService.class)
.withParams(body);
getNetData( body);
五、上傳文件
普通表單的上傳文件
File file = new File(fileUrl);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
@Multipart
@POST("xxxxx")
rx.Observable<ImageLoagBean> uploadFile(@Part MultipartBody.Part file, @Header("fileName")String
fileName, @Header("tokenId")String tokenId);
表單的方式上傳文件攜帶參數(shù)
RequestBody block = RequestBody.create(MediaType.parse("text/plain"), "client");
RequestBody token = RequestBody.create(MediaType.parse("text/plain"),String.valueOf(Hawk.get(AppConfig.Token,"")));
/**
* 上傳文件
*/
@Multipart
@POST("system/webservice/file/upload")
rx.Observable<UpLoadFileBean> uploadFile(@Part MultipartBody.Part file, @Part("token") RequestBody token ,
@Part("visitor_id") RequestBody visitor_id, @Part("block") RequestBody block );
binary方式上傳
這種方式是沒(méi)有鍵值對(duì)的與表單上傳的方式不同掰担,文件以二進(jìn)制的方式直接放到body里進(jìn)行上傳
客戶端接口代碼
客戶端代碼實(shí)現(xiàn)