Retrofit上传头像

 HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
        mRetrofit = new Retrofit.Builder().client(okHttpClient)
                .baseUrl(mBaseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //先创建 service
        FileUploadService service = mRetrofit.create(FileUploadService.class);

        //构建要上传的文件
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("application/otcet-stream"), file);/*application/otcet-stream*/

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

        String descriptionString = "This is a description";
        RequestBody description =
                RequestBody.create(
                        MediaType.parse("multipart/form-data"), descriptionString);/*application/x-www-form-urlencoded*/

        Call call = service.upload(description, body,mToken);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call,
                                   Response response) {
                System.out.println("--------success"+response.body().toString());
                showLoadingSuccess();
                mCivImg.setImageBitmap(mBitmap);
                EventBus.getDefault().post(new FixImage(mBitmap));
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                t.printStackTrace();
                showLoadingSuccess();
                ToastUtil.showToast(PersonalInformationActivity.this,"上传头像失败",0);
                System.out.println("--------success1111111");
            }
        });

你可能感兴趣的:(Retrofit上传头像)