1.在根目录下最底部:
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'
}
2.在model下:
// 网络请求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"
//或者
compile 'com.squareup.retrofit2:retrofit:2.1.0'//retrofit
compile 'com.google.code.gson:gson:2.6.2'//Gson 库
//下面两个是RxJava 和RxAndroid
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'//转换器,请求结果转换成Model
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'//配合Rxjava 使用
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0' //拦截日志
3.先创建一个Api
import java.util.Map;
import okhttp3.ResponseBody;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
import rx.Observable;
public interface BaceApis<T> {
//get
@GET
Observable<ResponseBody> get(@Url String url);
//post
@POST
Observable<ResponseBody> post(@Url String url, @QueryMap Map<String,String> map);
//图片文字上传
@POST
@Multipart
Observable<ResponseBody> postImageContent(@Url String url ,@QueryMap Map<String, String> map ,@Part MultipartBody.P