Retrofit @body with @multipart having Issue

后端 未结 8 1688
星月不相逢
星月不相逢 2020-12-25 08:54

Image Multipart in class type object.

case 1. (Which I had done)

Service params:

{\"id\":\"1\",\"name\":\"vishal\",\"image/file\":\"\"} 
         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-25 09:02

    As simple way, I have done like this:

    I have tested by changing

    Call resultCall = service.uploadImage(body); 
    

    to

    Call resultCall = service.uploadImage(body, result); where result is

    Result.java class (Response) of my API:

    public class Result {
    
        @SerializedName("result")
        @Expose
        private String result;
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String value) {
            this.value = value;
        }
    
        @SerializedName("value")
        @Expose
        private String value;
    
        /**
         * @return The result
         */
        public String getResult() {
            return result;
        }
    
        /**
         * @param result The result
         */
        public void setResult(String result) {
            this.result = result;
        }
    
    }
    

    and created object like:

    Result result = new Result();
    result.setResult("success");
    result.setValue("my value");
    

    You can change your class as per your need then pass object when you send request. So your ApiService class will be like:

    ApiService.java

    /**
     * @author Pratik Butani on 23/4/16.
     */
    public interface ApiService {
    
        /*
        Retrofit get annotation with our URL
        And our method that will return us the List of Contacts
        */
        @Multipart
        @POST("upload.php")
        Call uploadImage(@Part MultipartBody.Part file, @Part("result") Result result);
    
    }
    

    and My PHP code is:

     "success", "value" => $var);
        } else{
            $result = array("result" => "error");
        }
    
        echo json_encode($result);
    
    ?>
    

    Hope it will helps you. Thank you.

提交回复
热议问题