• 【Android】RxJava+Retrofit+OKHttp3实现数据上传


    依赖引入
    //Gson
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    //OkHttp3
    implementation 'com.squareup.okhttp3:okhttp:4.4.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'
    //Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.6.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
    //RxAndroid
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    封装
    public class RetrofitClient {
        private static volatile RetrofitClient retrofitClient;
        private Retrofit retrofit;
        private OkHttpClient okHttpClient;
        private ApiService apiService;
        private static final String baseUrl = "https://www.baidu.com/";
        private RetrofitClient() {}
        //单例
        public static RetrofitClient getInstance() {
            if (retrofitClient == null) {
                synchronized (RetrofitClient.class) {
                    if (retrofitClient == null) {
                        retrofitClient = new RetrofitClient();
                    }
                }
            }
            return retrofitClient;
        }
        //okhttp设置
        private OkHttpClient getOkHttpClient() {
            if (null == okHttpClient) {
                okHttpClient = new OkHttpClient.Builder()
                        .connectTimeout(10, TimeUnit.SECONDS)//连接超时
                        .readTimeout(10, TimeUnit.SECONDS)//读取超时
                        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))//设置日志拦截器
                        .build();
                okHttpClient.writeTimeoutMillis();
                okHttpClient.followRedirects();
            }
            return okHttpClient;
        }
        //Retrofit设置
        private Retrofit getRetrofit() {
            if (null == retrofit) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(baseUrl)
                        .addConverterFactory(GsonConverterFactory.create())//设置数据解析器
                        .client(getOkHttpClient())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//网络适配,支持RxJava与RxAndroid
                        .build();
            }
            return retrofit;
        }
        //接口
        public ApiService getApiService() {
            if (null == apiService)
                apiService = getRetrofit().create(ApiService.class);
            return apiService;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    API接口
    public interface ApiService {
        // 因为上面的方法封装中把url写全了,这里的post里面写个【/】,这里不能为空
        @POST("/")
        Observable<数据Bean> postBeanData(@Body RequestBody requestBody);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    使用
    //首先创建一个Bean保存数据
    String json = new Gson.toJson(数据Bean);
    //代码调用
    ApiService apiService = RetrofitClient.getInstance().getApiService();
    MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
    RequestBody requestBody = RequestBody.create(mediaType, json);
    apiService.postBeanData(requestBody).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<SensorBean>() {
                @Override
                public void onSubscribe(Disposable d) {
    
                }
                @Override
                public void onNext(SensorBean value) {
                    Log.e("api", "上传的数据:" + value.toString());
                }
                @Override
                public void onError(Throwable e) {
                    Log.e("api", "上传错误信息返回:" + e.toString());
                }
                @Override
                public void onComplete() {
                }
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    俄罗斯方块游戏开发教程8:下落处理
    【RTOS学习】精简RTOS源码 | 认识RTOS | 任务的创建和删除
    【历史上的今天】11 月 11 日:腾讯成立;信息论先驱出生;阿德曼提出 DNA 计算
    网络安全——(黑客)自学
    在香橙派OrangePi 3 LTS开发板上安装向日葵远程控制软件的操作方法
    并查集应用
    LeetCode 2034. 股票价格波动:哈希表 + 有序集合
    常见DOM操作
    如何在Windows下创建Ramdisk
    java计算机毕业设计计算机类专业考研交流学习平台MyBatis+系统+LW文档+源码+调试部署
  • 原文地址:https://blog.csdn.net/qq_43358469/article/details/128023435