首页 > 代码库 > OkHttp3 + retrofit2 封装

OkHttp3 + retrofit2 封装

0.下载文件

1.gradle 添加

compile ‘com.squareup.retrofit2:retrofit:2.1.0‘
compile ‘com.squareup.retrofit2:converter-gson:2.1.0‘
compile ‘com.squareup.retrofit2:adapter-rxjava:2.1.0‘

compile ‘com.squareup.okhttp3:okhttp:3.4.1‘
compile ‘com.squareup.okio:okio:1.9.0‘

 

2.AndroidManifest.xml 权限   

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

3.Application中初始化 

HttpUtil.getInstance().init(this, "http://baiqi.ej-cloud.com:8070/iot/");

4.调用

                HttpUtil.getInstance().get(
                        new HttpUtil.Builder()
                                .url("user/token/init")
                                .params("key", "1")
                                .params("type", "4")
                                .callBackSuccess(new CallBackSuccess() {
                                    @Override
                                    public void onSuccess(String json) {
                                        TopicResponse topicResponse = JsonUtil.fromJson(json, TopicResponse.class);
                                        tv.setText(topicResponse.data.id);
                                    }
                                }));

 

参考:

https://github.com/gengqiquan/HttpUtil

https://github.com/hongyangAndroid/okhttputils

OkHttp3 + retrofit2 封装