1.导包
compile'io.reactivex:rxjava:1.0.14'
compile'io.reactivex:rxandroid:1.0.1'
compile'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile'com.squareup.retrofit:converter-gson:2.0.0-beta2'
2.创建ApiService
url式例
http://gc.ditu.aliyun.com/geocoding?a=湖南省&aa=耒阳市&aaa=耒阳市
@FormUrlEncoded
@POST("geocoding?")
Observable getAdress(@Field("a") String a,@Field("aa") String aa,@Field("aaa") String aaa);
3.创建管理Retrofit的RetrofitManager
//单例模式
private static finalRetrofitManageretrofitManage=newRetrofitManage();
privateRetrofitManage(){
}
public staticRetrofitManage getInstance(){
returnRetrofitManage.retrofitManage;
}
public Retrofit CreateRetrofit(String url){
Retrofit retrofit=newRetrofit
.Builder()
.baseUrl(url)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
4.直接调用
ApiService service=staticRetrofitManage.getInstance().CreateRetrofit(url).create(ApiService.class);
Observable observer = service.getAdress("湖南省","衡阳市","耒阳市");
observer.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(newAction1() {
@Override
public voidcall(AliAdress aliAdress) {
//
}
});