Url类public class Api { public static final String USER_URL="http://mnews.gw.com.cn/"; } 请求类 get postpublic interface ApiService { /* @GET("wap/data/news/txs/page_1.json") Observable<List<Bean>> getBean();*/ @GET Observable<List<Bean>> getBean(@Url String url); }Retrofit+okpublic class RetroFactory { private static OkHttpClient httpClient = new OkHttpClient.Builder() .addInterceptor(new LoggingInterceptor()).connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .build(); private static ApiService retrofitService = new Retrofit.Builder() .baseUrl(Api.USER_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(httpClient) .build() .create(ApiService.class); //单列模式 public static ApiService getInstance() { return retrofitService; } }实例单例使用Observable<List<Bean>> bean = RetroFactory.getInstance().getBean(url);
Retrofit+okhttp拦截器获取网络数据
最新推荐文章于 2024-06-26 21:42:29 发布