android - Retrofit 设置上传图片类型为image/png 后台接收类型却为Application/json?
怪我咯
怪我咯 2017-04-18 09:03:51
0
1
1383

File imageFile = new File(imagePath);

    ToolUtils.Log("imageFile = "+imageFile.getPath());
    String imageName = splitPath(imagePath);
    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),imageFile);
    //RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"),imageFile);
    
    两种方式构建RequestBody对象php后台得到的type都为applicattion/json
    是否是因为.addConverterFactory(GsonConverterFactory.create())的原因?
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
巴扎黑

Check your Retrofit version. The way to upload images has changed in version 2.X. Use MultipartBody.Partas follows

RequestBody requestFile =RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =MultipartBody.Part.createFormData("image", file.getName(), requestFile);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template