Retrofit实现文件上传/图片上传/Json字符串上传

上传图片

配置依赖

ext {
    versionName ='1.0.0'
    androidutilsVersion = '1.0.6'
    appcompatVersion = '27.1.1'
    butterknifeVersion = '8.8.1'
    retrofitVersion = '2.4.0'
    logginginterceptor = '3.9.1'
    rxjavaVersion = '2.1.14'
    rxandroidVersion = "2.0.2"
    rxlifecycle='2.1.0'
}
dependencies {
    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.okhttp3:logging-interceptor:$logginginterceptor"
    implementation "com.squareup.retrofit2:converter-scalars:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"
    implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:latest.integration'
    implementation "io.reactivex.rxjava2:rxandroid:$rxandroidVersion"
    implementation "io.reactivex.rxjava2:rxjava:$rxjavaVersion"
}

API服务类:

public interface ApiStores {
 @Multipart
    @POST("core/wechatpublic/upload")
    Observable<UploadImageBean> uploadImage(@Part MultipartBody.Part file);
}

使用:

private void uploadFileThree() {
        //服务器地址
        String servicePath = "http://47.103.47.46:9762/";
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(servicePath)
                .build();
        ApiStores apiStores = retrofit.create(ApiStores.class);
        //拼接参数
        RequestBody requestBody = null;
        //测试使用sd卡的图片
        String imgPath = Environment.getExternalStorageDirectory().getPath() + "/icon_guanlian@3x.png";
        File file = new File(imgPath);
        requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        Observable<String> observable = apiStores.uploadFeedbackTwo(body);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                //只需要关心onNext的消息和Throwable错误
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
//                        LogUtils.d("onNext=上传成功" + s);

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
//                        LogUtils.d("throwable=" + throwable.getMessage());
                    }
                });
    }

 

上传Json

@POST("/uploadJson")
Observable<ResponseBody> uploadjson(
        @Body RequestBody jsonBody);

upLoadJson 也可以具体指明Content-Type 为 “application/json”格式的

具体组装我们的RequestBody则可以这样:

 RequestBody body= 
RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonString);

接着可以这样调用:

// 执行请求
Call<ResponseBody> call = service.uploadJson(description, body);
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call,
                           Response<ResponseBody> response) {
        Log.v("Upload", "success");
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e("Upload error:", t.getMessage());
    }
});

 

至于服务器返回什么类型的model, 开发者可以自定义 譬如你可以把APi 中的 ResponseBody 指定为你自己的javaBean,当然上层构建Callback的时候也必须是 Call<MyBean>

@POST("/uploadJson")
Observable<MyBean> uploadjson(
        @Body RequestBody jsonBody);

参考:http://blog.csdn.net/sk719887916/article/details/51755427

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值