Android新优化retrofit+rxjava

retrofit2 post 文字放在Body里面

2018-08-23  本文已影响920人  一个冬季
方案1:请求链接带参数,文字信息放Body
方案2:文字图片一起传递
方案3:全部都是body上传文字
方案4:全部都是body上传文字(带有结构的格式)
方案5:请求链接带参数,body含有文字,并且上传图片

方案1:请求链接带参数,文字信息放Body
后台开发人员说要采用post请求,然后文字要放在body里面的,链接后面也有参数。比如这样
xxxx.com?userId=8948&passkey=jfeowijfew

    api
    @Multipart
    @POST("xxxxx.php")
    Observable<FrdMsgDto> sendFrdMsg(@QueryMap HashMap<String,String> paramsMap,@Part("Msg") RequestBody Msg);

paramsMap 里面存放的是链接后面的参数
Msg 存放的是body里面的参数

  HashMap map = new HashMap<String, String>();
        map.put("userId",8948+"");
        map.put("passkey",jfeowijfew);
        RequestBody firstBody = RequestBody.create( MediaType.parse("multipart/form-data"), Msg);
  //请求服务器
 api.sendFrdMsg(map,firstBody)

方案2: 文字图片一起传递 参考文章

@Multipart
@POST("/xxx")
Observable<Result<String>> addInfo(@Part List<MultipartBody.Part> requestBodyMap);
       List<MultipartBody.Part> parts = new ArrayList<>();
        parts.add(toRequestBodyOfText("name", userName));//文字
        parts.add(toRequestBodyOfImage("image",foundImage));//图片
 //文字
private MultipartBody.Part toRequestBodyOfText (String keyStr, String value) {
        MultipartBody.Part body = MultipartBody.Part.createFormData(keyStr, value);
        return body;
    }
    //keyStr决定了你要上传到服务器的名称
    //pFile.getName() 表示文件的名称
   //图片
    private MultipartBody.Part toRequestBodyOfImage(String keyStr, File pFile){
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), pFile);
        MultipartBody.Part filedata = MultipartBody.Part.createFormData(keyStr, pFile.getName(), requestBody);
        return filedata;
    }

方案3:全部都是body上传文字

    @FormUrlEncoded
    @POST("test.php")  
    Observable<MessageBean> postAnnouncement3(@FieldMap Map<String,Object> map);
    Map<String,Object> map = new HashMap<>();
    map.put("userName","zzzzz");
    api.postAnnouncement3(map);

方案4:全部都是body上传文字(带有结构的格式)
接口:

    @FormUrlEncoded
    @POST("xxxx.php")  
    Observable<Bean> postment(@FieldMap Map<String,String> map);

后台需要的格式为

{
  key0:value0,
  key1:value1,
  user:{
    "sex":1,
    "name":"张三"
  }
}

看到这样的方式,首先我想到的是采用@FieldMap Map<String,String> map 来上传

Map<String,String> map = new HashMap<String,String>
map.put("key0","value0");
map.put("key1","value1");
UserBean user = new UserBean();
user.setSex(1);
user.setName("张三");
map.put("user",user.toString());

结果我发现上述方法后台无法获取user里面的数据
又改

Map<String,Object> map = new HashMap<String,Object>
map.put("key0","value0");
map.put("key1","value1");
Map<String,Object> objMap= new HashMap<String,Object>
objMap.put("sex",1);
objMap.put("name","张三");
map.put("user",objMap);

然后我又发现上述方法还是无法让后台获取user里面的数据
此时的我


aaa.jpg

怎么办好捉急啊,后面后台PHP开发人员说拼凑好了,OK,那行吧

Map<String,String> map = new HashMap<String,String>
map.put("key0","value0");
map.put("key1","value1");
map.put("user[sex]",1);
map.put("user[name]","张三");

当时第一次使用这样的格式的时候,我还是懵逼的,怎么还有这样的操作?但是还真能拼凑出后台需要的格式,突然我就不知道要说啥了。哎


dddd.jpg

方案5:请求链接带参数,body含有文字,并且上传图片

接口:
   //注意我使用了@QueryMap   @PartMap
    @Multipart
    @POST("aaaaPhoto.php")
    Observable<MessageBean> postNewCompanyLicence(@QueryMap Map<String,String> map,
                                                  @Part MultipartBody.Part photo1,
                                                  @Part MultipartBody.Part photo2,
                                                  @PartMap Map<String,RequestBody> map2);
   //....省去代码
       Map<String, String> map = new HashMap<String, String>(1);
        map.put("ObjectRoleID", "1234");
        Map<String, RequestBody> map2 = new HashMap<String, String>(7);
        map2.put("B_Photo_W", toRequestBodyOfText(bean.getB_Photo_W() + ""));
        map2.put("B_Photo_H", toRequestBodyOfText(bean.getB_Photo_H() + ""));
        map2.put("S_Photo_W", toRequestBodyOfText(bean.getS_Photo_W() + ""));
        map2.put("S_Photo_H", toRequestBodyOfText(bean.getS_Photo_H() + ""));
        map2.put("UserID", toRequestBodyOfText("aaaa"));
        map2.put("PassKey", toRequestBodyOfText("bbbb")));
        map2.put("PhotoAlbumID",toRequestBodyOfText("ccccc"));

        File bigFile = new File(bean.getBigPicPath());//图片的地址
        File smlFile = new File(bean.getSmlPictPath());
        RequestBody bigImageBody0 = RequestBody.create(MediaType.parse("multipart/form-data"), bigFile);
        RequestBody smlImageBody0 = RequestBody.create(MediaType.parse("multipart/form-data"), smlFile);
        //这里的Photo  S_Photo是作为key
        MultipartBody.Part bigPart = MultipartBody.Part.createFormData("Photo", bigFile.getName(), bigImageBody0);
        MultipartBody.Part smlPart = MultipartBody.Part.createFormData("S_Photo", smlFile.getName(), smlImageBody0);
        api.postNewCompanyLicence(map, bigPart, smlPart, map2)



        public RequestBody toRequestBodyOfText (String value) {
        RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
        return body ;
    }
   //....省去代码
上一篇下一篇

猜你喜欢

热点阅读