附加文件而不是字符串数据时,RequestBody始终为null

时间:2019-05-06 04:53:45

标签: java android retrofit2

我正在使用Retrofit 2.5版,我已经看到了所有与它有关的文档和问题,但是我还没有找到任何解决方案。这里的问题是,附加文件后,文件的RequestBody始终为null。我在这里附上了我的代码,请看一下-

          final RequestBody msgbody = RequestBody.create(MediaType.parse("text/plain"), messege);
                final RequestBody idbody = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(id));
                //map.put("userId", idbody);
                //map.put("message", msgbody);
                RequestBody fbody = null;
                if (fileUri!=null) {
                    String path = FileUtils.getPath(getContext(),fileUri);
                    File file = new File(path);
                    fbody = RequestBody.create(MediaType.parse(getContext().getContentResolver().getType(fileUri)), file);
                    //RequestBody body = RequestBody.create(MediaType.parse("*/*"), file);
                    //fbody = MultipartBody.Part.createFormData("file", file.getName(), body);
                    Log.e("SUPPORT",path+" body:"+new Gson().toJson(fbody));
                    //map.put("file\"; filename=\""+file.getName()+"\"",body);
                }

                Call<SubmitTicket> call = mInterface.issueTicket(idbody,msgbody,fbody);
                Log.e("SUPPORT",new Gson().toJson(call));
                call.enqueue(new Callback<SubmitTicket>() {
                    @Override
                    public void onResponse(Call<SubmitTicket> call, Response<SubmitTicket> response) {
                        loadingBar.setVisibility(View.GONE);
                        submitBtn.setEnabled(true);
                   }
                   @Override
                   public void onFailure(Call<ResponseModel> call, Throwable t) {
                      t.printStackTrace();
                      Log.i("Error :",t.getCause()+"");
                      Log.i("Error :","T");
                      finish();
                   } //error even before compile**
            });

这是界面代码,

@Multipart
@POST("Support")
//Call<SubmitTicket> issueTicket(@PartMap Map<String, RequestBody> params);
Call<SubmitTicket> issueTicket(@Part("userId") RequestBody id, @Part("message") RequestBody messege, @Part("image\"; filename=\"myfile.jpg\" ") RequestBody file);

感谢所有答案和评论。

2 个答案:

答案 0 :(得分:1)

使用此功能,对您有帮助。您需要以多部分形式发送数据。

您的界面将如下所示:

 @Multipart
 @POST("Support")
 Call<SubmitTicket> issueTicket(@Part("userId") RequestBody id, @Part("message") RequestBody messege, @Part MultipartBody.Part file);

您的请求将如下所示:

  RequestBody msgbody = RequestBody.create(MediaType.parse("text/plain"), "yourMessage");
        RequestBody idbody = RequestBody.create(MediaType.parse("text/plain"), "id");


        if (fileUri!=null) {
            String path = FileUtils.getPath(getContext(),fileUri);
            File file = new File(path);

            // create RequestBody instance from file
            RequestBody requestFile =
                    RequestBody.create(MediaType.parse("multipart/form-data"), file);

            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("file", file.getName(), requestFile);



        Call<SubmitTicket> call = mInterface.issueTicket(idbody,msgbody,body);
        Log.e("SUPPORT",new Gson().toJson(call));
        call.enqueue(new Callback<SubmitTicket>() {
            @Override
            public void onResponse(Call<SubmitTicket> call, Response<SubmitTicket> response) {
                loadingBar.setVisibility(View.GONE);
                submitBtn.setEnabled(true);
            }
            @Override
            public void onFailure(Call<ResponseModel> call, Throwable t) {
                t.printStackTrace();
                Log.i("Error :",t.getCause()+"");
                Log.i("Error :","T");
                finish();
            } //error even before compile**
        });

        }

这一直在帮助我。希望这些对您和其他人也有用。

答案 1 :(得分:1)

首先,您需要在界面中进行更改。代替使用@Part RequestBody file,而按如下方式使用@Part MultipartBody.Part file-

@Multipart
@POST("Support")
Call<SubmitTicket> issueTicket(@Part("userId") RequestBody id, @Part("message") RequestBody messege, @Part MultipartBody.Part  image);

然后在您的请求部分中使用Mulitpart.Part正文而不是RequestBody作为 跟随-

MultipartBody.Part fbody = null;
                if (fileUri!=null) {
                    String path = FileUtils.getPath(getContext(),fileUri);
                    File file = new File(path);
                    //here **image** is your remote file param name
                    fbody = MultipartBody.Part.createFormData("image", file.getName()+System.currentTimeMillis(), RequestBody.create(MediaType.parse("image/*"), file));

                    Log.e("SUPPORT",path+" body:"+new Gson().toJson(fbody));
                    //map.put("file\"; filename=\""+file.getName()+"\"",body);
                }