POST Api请求无法使用Retrofit正常工作

时间:2019-06-25 07:27:12

标签: java android retrofit2

我尝试使用Retrofit进行发布请求,如下所示:

@Headers("Accept: application/json")
@Multipart
@POST("messages/382/image")
Call<Void> sendImage(@Part MultipartBody.Part filePart,@HeaderMap Map<String, String> headers);

但是,它似乎无法正常工作。 任何帮助将不胜感激。

这就是我创建请求的方式;

RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

现在我有回应:{protocol=http/1.1, code=415, message=Unsupported Media Type

但是当我这样做时,它可以正常工作:

public void sendImage(String messageId, File pictureUri, String token, AsyncHttpResponseHandler handler) {
   BasicHeader[] headers = new BasicHeader[]{new BasicHeader("Authorization", "SCToken " + token)};
   //Logger.d("picture Url: " + pictureUri);
   ImageLoader imageLoader = ImageLoader.getInstance();
   FileEntity fileEntity = null;
   try {
     fileEntity = new FileEntity(pictureUri, "image/jpeg");
   } catch (Exception e) {
     e.printStackTrace();
   }
   client.post(context, getAbsoluteUrl("messages/" + messageId + "/image"), headers, fileEntity, "image/jpeg", handler);
}

0 个答案:

没有答案
相关问题