Retrofit上传头像

来源:互联网 发布:mac tomcat端口被占用 编辑:程序博客网 时间:2024/06/07 21:02
 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<ResponseBody> call = service.upload(description, body,mToken);        call.enqueue(new Callback<ResponseBody>() {            @Override            public void onResponse(Call<ResponseBody> call,                                   Response<ResponseBody> response) {                System.out.println("--------success"+response.body().toString());                showLoadingSuccess();                mCivImg.setImageBitmap(mBitmap);                EventBus.getDefault().post(new FixImage(mBitmap));            }            @Override            public void onFailure(Call<ResponseBody> call, Throwable t) {                t.printStackTrace();                showLoadingSuccess();                ToastUtil.showToast(PersonalInformationActivity.this,"上传头像失败",0);                System.out.println("--------success1111111");            }        });