1、引用所使用的工具
implementation 'com.squareup.retrofit2:retrofit:2.2.0' implementation 'com.squareup.retrofit2:converter-gson:2.2.0' implementation 'com.squareup.retrofit2:adapter-rxjava:2.2.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0' implementation 'com.squareup.okhttp3:okhttp:3.2.0'
2、添加请求NetInterface
/** * 图片上传 * @param imageName * @param file * @return */ @Multipart @POST("/unified/service/innerReceiveImage") Observable<BeasResponse> uploadPic(@Query("imageName") String imageName, @Part MultipartBody.Part file); /** * 请求接口 * * @param businessType * @param reqData * @return */ @FormUrlEncoded @POST("/unified/service/creditCardService") Observable<BeasResponse> reqeuest(@Field("accessKey") String accessKey, @Field("businessType") String businessType, @Field("reqData") String reqData);
3、网络请求类的封装(NetInterface的使用)
/**
* 图片上传
* @param imageName
* @param imagePath
* @return
*/
public Observable<BeasResponse> ApuploadPIC(String imageName,String imagePath){
File file = new File(imagePath);
RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
return observe(mNetInterface.uploadPic(imageName,body));
}
public Observable<BeasResponse> ApCheckCardList(CarCheckListRequet carCheckListRequet){
gson = new Gson();
String reqData = gson.toJson(carCheckListRequet);
LogUtil.d(TAG,"reqData = "+reqData);
return observe(mNetInterface.reqeuest("1193",reqData));