图片上传Retrofit+RxJava

时间:2023-01-20 17:45:02

RetrofitApi:

public interface RetorfitApi {

    @POST("file/upload")
    @Multipart
    Observable<Test> uploadFile(@Part("uid")RequestBody uid, @Part MultipartBody.Part file);
}

RetrofitUtils:

public class RetrofitUtils {
    static {
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addNetworkInterceptor(logInterceptor)
//                .addInterceptor()
                .readTimeout(50, TimeUnit.SECONDS)
                .writeTimeout(50, TimeUnit.SECONDS).build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        retorfitApi = retrofit.create(RetorfitApi.class);
    }
    public static RetorfitApi retorfitApi;

    private static class HttpLogger implements HttpLoggingInterceptor.Logger {

        @Override
        public void log(String message) {
            Log.e("TAG", message);
        }
    }
}

HttpUtils:

public class HttpUtils {
    public void uploadDataToServer(File f) {
        RequestBody uid = RequestBody.create(MediaType.parse("text/plain"), "自己的Uid");
        RequestBody file = RequestBody.create(MediaType.parse("application/octet-stream"), f);
        String name = f.getName();
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", name, file);

        Observable<Test> testObservable = RetrofitUtils.retorfitApi.uploadFile(uid, filePart);
        testObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Test>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Test test) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
MainPresenter
public class MainPresenter extends BasePresenter<IMainView> {
    private final HttpUtils httpUtils;
    private Context mContext;

    public MainPresenter() {
        httpUtils = new HttpUtils();
    }

    public void uploadData() {
        File dataDirectory = Environment.getExternalStorageDirectory();
        String uri = dataDirectory.toString() + "/003.png";
        File file = new File(uri);

        if (file.exists()) {
            Log.e("myMessage", "uri = " + file.getAbsolutePath() + " " + file.isFile());
        }

        httpUtils.uploadDataToServer(file);
    }

}