使用改造将Android图像上传到服务器

时间:2018-08-06 07:59:28

标签: java android retrofit

我的图片没有上传到服务器,我已经搜索了几个链接,但是没有找到以下有用的方式

RFInterface.java

@Multipart

 @POST("compose_notice")
    Call<PostNoticeModel> compose_notice(
            @Query("apartment_id") String apartment_id,
            @Query("subject") String subject,
            @Query("descriptions") String descriptions,
            @Query("expiry_time") String expiry_time,
            @Query("notice_visible_group") String notice_visible_group,
            @Query("selectedUser") String selectedUser,
            //@Part ("attachmentFile") RequestBody file
            //@Part ("attachmentFile\"; filename=\"attachmentFile\" ") RequestBody attachmentFile
            @Part MultipartBody.Part  attachmentFile

            );

这是我对rfinterface.java文件做出响应的代码

 btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                get_title =  edt_title.getText().toString();
               get_description = edt_description.getText().toString();
               if(get_expires.equals("Custom")){
                   get_expires = custom_date.getText().toString();
               }

                if(get_title.trim().length() == 0){
                    Toast.makeText(PostNotice.this, "Please enter title", Toast.LENGTH_SHORT).show();
                }else if(get_description.trim().length() == 0){
                    Toast.makeText(PostNotice.this, "Please enter description", Toast.LENGTH_SHORT).show();
                }else {

               if(finalfile != null) {
                   reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), finalfile);
                   body = MultipartBody.Part.createFormData("attachmentFile", finalfile.getName(), reqFile);
//                   get_titlee =
//                           RequestBody.create(MediaType.parse("multipart/form-data"), get_title);
               }else{
                    body = null;
               }
                    //Toast.makeText(PostNotice.this, ""+body, Toast.LENGTH_SHORT).show();

                    RFInterface api = RFClient.getApiService();
                    Call<PostNoticeModel> call = api.compose_notice(apartment_id, get_title, get_description, get_expires, get_noticevisibility, suserstring, body);
                    final ProgressDialog progressDoalog;
                    progressDoalog = new ProgressDialog(PostNotice.this);
                    progressDoalog.setMessage("Its loading....");
                    progressDoalog.setTitle("Please wait it take some time ");

                    // show it
                    progressDoalog.show();
                    call.enqueue(new Callback<PostNoticeModel>() {
                        @Override
                        public void onResponse(Call<PostNoticeModel> call, Response<PostNoticeModel> response) {
                            if (response.isSuccessful()) {
                                getresponse = response.body().getMsg();
                                Toast.makeText(getApplicationContext(), getresponse, Toast.LENGTH_LONG).show();
                                progressDoalog.dismiss();

                            } else {
                                Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
                                progressDoalog.dismiss();
                            }
                        }

                        @Override
                        public void onFailure(Call<PostNoticeModel> call, Throwable t) {
                            progressDoalog.dismiss();
                        }
                    });
                }
            }
        });

请告诉我我在哪里做错了所有事情都正常工作,所有数据都插入表中以撰写通知,但图像未上传,图像列仍为空。

1 个答案:

答案 0 :(得分:0)

使用Requesbody

@多部分     @POST(urlString)     有趣的addField(@PartMap partMap:地图,                  @Part图像文件:MultipartBody.Part?     ):可观察

相关问题