问题描述
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())的原因?
问题解答
回答1:查看下你的Retrofit的版本,2.X的版本中上传图片的方式发生了变化,使用MultipartBody.Part如下
RequestBody requestFile =RequestBody.create(MediaType.parse('multipart/form-data'), file);// MultipartBody.Part is used to send also the actual file nameMultipartBody.Part body =MultipartBody.Part.createFormData('image', file.getName(), requestFile);