导入依赖
app下的gradle
// 网络请求Retrofit & OkHttp & Rxjava
implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.interceptor"
implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofit"
implementation "com.squareup.retrofit2:converter-gson:$rootProject.converterGson"
implementation "io.reactivex:rxandroid:$rootProject.rxandroid"
implementation "io.reactivex:rxjava:$rootProject.rxjava"
implementation "com.hwangjr.rxbus:rxbus:$rootProject.rxbus"
implementation "com.squareup.retrofit2:adapter-rxjava:$rootProject.adapterRxjava"
//okhttp依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
根目录下的gradle
ext {
// Sdk and tools
minSdkVersion = 19
targetSdkVersion = 26
compileSdkVersion = 26
buildToolsVersion = '26.0.2'
//support版本
supportVersion = '27.1.0'
constraintLayout = '1.0.2'
junit = '4.12'
testRunner = '1.0.1'
espressoCore = '3.0.1'
interceptor = "3.11.0"
retrofit = '2.3.0'
converterGson = '2.3.0'
rxandroid = '1.2.1'
rxjava = '1.3.0'
rxbus = '1.0.6'
adapterRxjava = '2.0.2'
}
创建类如下图 分别为MVP层
BaseApis
import java.util.Map;
import okhttp3.ResponseBody;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
import rx.Observable;
public interface BaseApis<T> {
@GET
Observable<ResponseBody> get(@Url String urlData);
@POST
Observable<ResponseBody> post(@Url String urlData, @QueryMap Map<String,String> map);
@DELETE
Observable<ResponseBody> delete(@Url String urlData,@QueryMap Map<String,String> map);
@PUT
Observable<ResponseBody> put(@Url String urlData,@QueryMap Map<String,String> map);
}
Apis
放入接口后缀,例如:
public static final String Path="接口";
Retrofit
import java.util.Map;
import java.util.concurrent.TimeUnit;
import asus.com.qing.jjyk1.BaseApis;
import okhttp3.OkHttpClient;
import okhttp3.Requ