Retrofit上传头像

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://120.27.23.105/")//网址
        .addConverterFactory(GsonConverterFactory.create())//Gson工厂类
        .build();
GitHubApi api = retrofit.create(GitHubApi.class);// 自定义接口对象
//创建文件对象,传入文件地址
File file = new File("/storage/emulated/0/Pictures/Screenshots/a.jpg");
System.out.println("文件路径为:"+file.getAbsolutePath());
// 将文件对象传入RequestBody对象中
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
// 将RequestBody对象传入MultipartBody对象中(1.后台需要的key类型 2.文件名称 3.存有文件对象的RequestBody对象)
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
// 文件描述对象
final RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), "这是文件描述");
api.upLoading(requestBody,body,"313").enqueue(new Callback<UpLoadBean>() {
    @Override
    public void onResponse(Call<UpLoadBean> call, Response<UpLoadBean> response) {
        System.out.println(response.body().getMsg()+"------------");
    }

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

    }

});

接口类:

@Multipart
@POST("file/upload")
Call<UpLoadBean> upLoading(@Part("description") RequestBody description, @Part MultipartBody.Part file,@Query("uid") String uid);
导入依赖:
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值