首先是API地址:
public static final String PRO_URL="http://result.eolinker.com/";
然后就是简单的封装类了:
RetroFactory
public class RetroFactory { private RetroFactory() { } private static OkHttpClient httpClient = new OkHttpClient.Builder() .addInterceptor(new LoggingInterceptor()).connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .build(); private static ApiServer retrofitService = new Retrofit.Builder() .baseUrl(Api.PRO_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(httpClient) .build() .create(ApiServer.class); //单列模式 public static ApiServer getInstance() { return retrofitService; }}
然后在ApiServer接口里面调用:
@GET("umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage") ObservablegetHome();
这个接口不一定能用粘贴完记得换上自己的接口;
然后是使用了;
public void initData(String name, String pass, final LoginIsOk lk){ ApiServer instance = RetroFactory.gehome(); Observablesetname = instance.setname(name,pass); setname.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer () { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(LoginBean loginBean) { Log.d("maina",loginBean.toString()); String code = loginBean.getCode(); lk.loginIsOk(code,loginBean.getMsg(),loginBean.getData().getUid()+""); } }); }
这样就可以使用了,具体Retrofit请求,请看本人前面的博客;