使用改造将文件上传到服务器

时间:2018-04-30 09:18:26

标签: android retrofit

如果对服务器的请求在图像中看起来如此,如何从改进的android发出请求 enter image description here

1 个答案:

答案 0 :(得分:0)

嗯,你可以在发布之前搜索一下,确定你已经找到了一些东西,但是......

@Multipart
@POST(RestClient.UPDATE_USER_PICTURE)
Call<BooleanResponse> updateUserPicture(@Part MultipartBody.Part picture, @Part("picture") RequestBody file);

以上部分是您将使用的改装界面方法

RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), loadImageFile);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("picture", loadImageFile.getName(), reqFile);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), loadImageFile.getName());

RestClient.getApi().updateUserPicture(filePart, filename).enqueue(new Callback<BooleanResponse>() {
        @Override
        public void onResponse(Call<BooleanResponse> call, Response<BooleanResponse> response) {
            if (response.isSuccessful() && response.code() == 200){
                Toast.makeText(CompleteProfile.this, "Picture updated successfully!", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<BooleanResponse> call, Throwable t) {
            Toast.makeText(CompleteProfile.this, "Request failed !" + t.getMessage(), Toast.LENGTH_SHORT).show();
            Log.e(TAG, "-=onFailure=-\n" + t.getMessage(), t);
        }
    });