Android笔记(16)retrofit上传图片

1.接口

    @Multipart
    @POST("WorkerApi/User/uploadPersonPic")
    Call uploadPersonPic(@Part("worker_id") RequestBody worker_id, @Part MultipartBody.Part image);

2.Model

public Call uploadPersonPic(RequestBody worker_id, MultipartBody.Part image){
        Service service =retrofit1.create(Service.class);
        return service.uploadPersonPic(worker_id,image);
    }

3.Activity

final RequestBody worker_id =
                RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(loginBean.getData().getUser().getWorker_id()));
        RequestBody image =
                RequestBody.create(MediaType.parse("multipart/form-data"), file);
        final MultipartBody.Part body =
                MultipartBody.Part.createFormData("image", file.getName(), image);
                Call call1=new Model().uploadPersonPic(worker_id,body);
        call1.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {

            }

            @Override
            public void onFailure(Call call, Throwable t) {

            }
        });

你可能感兴趣的:(android)