项目中使用 Rxjava 进行网络请求,请求的方式有 get 请求和 post 请求,而请求中 有时候 须要 请求头 head,有时候须要 请求 json 串,琳琳种种,梳理下思路:java
#1. get 请求json
@GET("login/sys") ObservablegetTKUrl(); 复制代码
@GET("customer/login") Observable<PolicRecordBean> getPolicyRecord( @Query("id") String id, @Query("token") String token ); 复制代码
多个参数请求安全
@GET("News") Call<NewsBean> getItem( @Query("newsId") String newsId, @QueryMap Map<String, String> map); 复制代码
#2. post请求 -- 大部分为有参,增长安全性bash
##2.1 post 简单请求服务器
@POST("preservation/login") Observable<HeadBanner> getHeadBanner( @Query("token") String token ); 复制代码
##2.2 post 普通请求 Field 与 FormUrlEncoded 联用markdown
@FormUrlEncoded @POST("preservation/login") Observable<Loan> loan( @Field("param") String param, @Field("sign") String sign, @Query("token") String token ); 复制代码
@FormUrlEncoded @POST("cos/getPubSignParams") Observable<TencentCloudParams> getTencentCloudParams( @FieldMap Map<String, String> params, @Query("token") String token ); 复制代码
@FormUrlEncoded @POST("Comments/{newsId}") Call<Comment> reportComment( @Path("newsId") String commentId, @Query("access_token") String access_token, @Field("reason") String reason); 复制代码
@POST("cos/getSignParamsNew") Observable<TencentCloudKeyParamsBean> getTencentCloudParamsSet( @Body RequestBody body, @Query("token") String token ); 常见的写法 2.5.1 自定义 bean DBean request=new DBean (); request.setAA(aa); Gson gson = new Gson(); String param = gson.toJson(request); RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),param); HttpUtil.init(HttpUtil.sspHttpUtils().getCustomDetailsList(body, App.TOKEN), new Subscriber<GetCustomBean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(GetCustomBean getCustomBean) {} 2.5.2 使用jsonobject JSONObject jsonObject = new JSONObject(); try { jsonObject.put("AA", aa); } catch (JSONException e) { e.printStackTrace(); } RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString()); HttpUtil.init(HttpUtil.sspHttpUtils().login(requestBody), new Subscriber<DBean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(DBean bean) {} 2.5.3 使用map Map map=new HashMap(); map.put("AA",aa); RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(map)); HttpUtil.init(HttpUtil.sspHttpUtils().login(requestBody), new Subscriber<DBean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(DBean bean) {} 复制代码
@Headers 用于添加固定请求头,能够同时添加多个。经过该注解添加的请求头不会相互覆盖,而是共同存在 @Header 做为方法的参数传入,用于添加不固定值的Header,该注解会更新已有的请求头网络
@Headers("secret:AA") @POST("customer/login") Observable<DBean> getDB( @Body RequestBody body, @Query("token") String token, @Query("sign") String sign ); @Headers("Cache-Control: max-age=120") @GET("请求地址") Observable<HttpResult> getInfo(); 或者 @GET("请求地址") Observable<HttpResult> getInfo(@Header("token") String token); 复制代码
使用Retrofit+RxJava实现网络请求 www.jianshu.com/p/092452f28…app
1)上传单个文本和单个文件 @Multipart @POST("请求地址") Observable<HttpResult> upLoadTextAndFile(@Part("textKey") RequestBody textBody, @Part("fileKey\"; filename=\"test.png") RequestBody fileBody); 第一个参数用于传文本, > --- @Part("textKey")中的"textKey"为文本参数的参数名。 > --- RequestBody textBody为文本参数的参数值,生成方式以下: > RequestBody textBody = RequestBody.create(MediaType.parse("text/plain"), text); 第二个参数用于传文件, > --- @Part("fileKey"; filename="test.png") > 其中的"fileKey"为文件参数的参数名(由服务器后台提供) > 其中的"test.png"通常是指你但愿保存在服务器的文件名字,传入File.getName()便可 > --- RequestBody fileBody为文件参数的参数值,生成方法以下: > RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), file); > > (这里文件类型以png图片为例,因此MediaType为"image/png", > 不一样文件类型对应不一样的type,具体请参考[http://tool.oschina.net/commons](http://tool.oschina.net/commons)) 复制代码
上传多个文本和多个文件(经过Map来传入)ide
@Multipart @POST("") Observable<HttpResult> upLoadTextAndFiles(@PartMap Map<String, RequestBody> textBodyMap, @PartMap Map<String, RequestBody> fileBodyMap); 第一个参数用于传文本, Map的key为String,内容请参考上方“上传文本和单个文件”中@Part()里的值。 Map的value值为RequestBody,内容请参考上方“上传文本和单个文件”中RequestBody的生成。 第二个参数用于传文件, Map的key为String,内容请参考上方“上传文本和单个文件”中@Part()里的值。 Map的value值为RequestBody,内容请参考上方“上传文本和单个文件”中RequestBody的生成。 复制代码
另外补充多一种上传方式(2018/07/16),以上传多个文本和多个文件为例oop
@POST("") Observable<HttpResult> upLoadTextAndFiles(@Body MultipartBody multipartBody); MultipartBody 的生成方式以下: MultipartBody.Builder builder = new MultipartBody.Builder(); //文本部分 builder.addFormDataPart("fromType", "1"); builder.addFormDataPart("content", "意见反馈内容"); builder.addFormDataPart("phone", "17700000066"); //文件部分 RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file); builder.addFormDataPart("image", file.getName(), requestBody); // “image”为文件参数的参数名(由服务器后台提供) builder.setType(MultipartBody.FORM); MultipartBody multipartBody = builder.build(); 下载文件 //下载大文件时,请加上@Streaming,不然容易出现IO异常 @Streaming @GET("请求地址") Observable<ResponseBody> downloadFile(); //ResponseBody是Retrofit提供的返回实体,要下载的文件数据将包含在其中 复制代码
part --- eg:
/** * 文本参数转换 * @param value * @return */ public static RequestBody toRequestBody(String value) { RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), value); return body; } @Multipart @POST("/Card") Observable<SubmitEditVisitingCardInfoBean> submitEditVisitingCardInfo(@Part("staffNumber") RequestBody staffNumber, @Part MultipartBody.Part headimg, @Part("workAge") RequestBody workAge, @Part("wxNum") RequestBody wxNum, @Part MultipartBody.Part wximg, @Part("tags") RequestBody tags, @Part("sex") RequestBody sex, @Part("proSeleIdList") RequestBody proSeleIdList); private MultipartBody.Part wxBodyP; private MultipartBody.Part headBodyP; File head = new File(currentHeadImgUri.getPath()); RequestBody headBoyd = RequestBody.create(MediaType.parse("multipart/form-data"), head); headBodyP = MultipartBody.Part.createFormData("headImgFile", "head.png", headBoyd); File wx = new File(currentRqCodeUri.getPath()); RequestBody wxBody = RequestBody.create(MediaType.parse("multipart/form-data"), wx); wxBodyP = MultipartBody.Part.createFormData("wx2DImgFile", "wxxx.png", wxBody); HttpUtil.init(HttpUtil.sspHttpUtils().submitCardInfo( toRequestBody(staf), headBodyP, toRequestBody(getWorkAge()), toRequestBody(getWxNum()), wxBodyP, toRequestBody(getTags()), toRequestBody(getSex()), toRequestBody(getSelectedRecommendId()) ), new Subscriber<CardInfoBean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(CardInfoBean card) {} 复制代码