服务器在Retrofit2文件上传后返回响应500

时间:2019-02-12 23:08:10

标签: java android rest retrofit2

我正在使用可与一堆Web服务一起使用的android应用程序。我正在将Retrofit2用于所有其他功能,并且几乎可以完美运行。除了上传文件外,我还发送文件(jpeg图像)和字符串(说明)。我从Web服务收到500响应,已经检查了日志并尝试查看应用程序发送的请求,但遗憾的是错误日志为空。似乎apache服务器正在拒绝请求,因为它可能格式错误。

接口定义如下

@Multipart
@POST("api/mobilesale/orders/{id}/documents/multipleUpload")
Call<ResponseBody> uploadDocument(@Header("Authorizarion") String token,
                                  @Part MultipartBody.Part file,
                                  @Path("id") int orderId,
                                  @Part("documentType") RequestBody document_type);

上传方法是这样定义的

private void upload(File file, String documentType, String token, int orderId)
{
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(LDLMobileDataService.BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
    MobileDataService serviceAPI = retrofit.create(MobileDataService.class);

    RequestBody requestBody = RequestBody.create(MediaType.parse(getContentResolver().getType(file_uri)), file);
    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("document", file.getName(), requestBody);
    RequestBody documentType = RequestBody.create(MediaType.parse("text/plain"), documentType);

    Call<ResponseBody> call = serviceAPI.uploadDocument("Bearer "+ token, fileToUpload, orderId, documentType);

    call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if(response.isSuccess())
                {
                    Toast.makeText(getApplicationContext(), "File Upload Success", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(getApplicationContext(), "File Upload Error", Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "File Upload Failure", Toast.LENGTH_LONG).show();
            }
        });
}

我想念什么?

0 个答案:

没有答案