网络框架封装 okhttprxjava(POST/GET 文件上传等)
githua地址: https://github.com/JI577/OkHttpRxjavaDemo.git依赖库: implementation ‘com.github.JI577:OkHttpRxjavaDemo:1.0’调用方式 (Kotlin)val hashMap = HashMap<Any, Any>()hashMap.put("id", "123")...
·
githua地址: https://github.com/JI577/OkHttpRxjavaDemo.git
依赖库: implementation ‘com.github.JI577:OkHttpRxjavaDemo:1.0’
调用方式 (Kotlin)
val hashMap = HashMap<Any, Any>()
hashMap.put("id", "123")
var createNoRequestBody = CreateRequestBodyUtil.createRequestBody(hashMap)
val homeTypeNet = apiService.getHomeTypeNet(createNoRequestBody)
RtRxOkHttp.getInstance().createRtRx(this, homeTypeNet, this, 1)
回调
override fun httpResponse(info: Object?, isSuccess: Boolean, sort: Int) {
if (isSuccess) {//接口连接成功
when (sort) {
1 -> {
val homeCateInfo = info as HomeCateInfo
Logger.e("jrq", "info:" + homeCateInfo.code)
}
}
} else {//接口访问失败
}
}
}
Api
//post
@Headers("Content-Type:application/x-www-form-urlencoded;charset=utf-8", "Accept:application/json;")
@POST("web/file/uploadImage")
fun getHomeTypeNet(@Body body: RequestBody): Observable<HomeCateInfo>
//图片上传
@Multipart
@POST("接口")
fun getUpdatePicNet(@Part file: MultipartBody.Part): Observable<String>
public class RtRxOkHttp<T, Q> {
private static Retrofit retrofit;
private static OkHttpClient okHttpClient;
private Q api;
private static RtRxOkHttp instance;
private static CallAdapter.Factory rxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();
public static RtRxOkHttp getInstance() {
if (instance == null) {
instance = new RtRxOkHttp();
}
return instance;
}
public void init(String http, Class<Q> apiClass) {
generateOkhttpClient();
retrofit = new Retrofit.Builder()
.baseUrl(http)
.client(okHttpClient)
.addCallAdapterFactory(rxJavaCallAdapterFactory)
.addConverterFactory(ResponseConverterFactory.create())
.build();
api = retrofit.create(apiClass);
}
public static void generateOkhttpClient() {
if (okHttpClient == null) {
okHttpClient = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
//time out
.readTimeout(6000 * 10, TimeUnit.SECONDS)
.connectTimeout(6000 * 10, TimeUnit.SECONDS)
.addInterceptor(new LoggingInterceptor())
// .addInterceptor(mInterceptor)
.build();
}
}
public Q getApiService(String http, Class<Q> apiClass) {
if (api == null) {
init(http, apiClass);
}
return api;
}
public Subscription createRtRx(final Context activity, Observable<T> postHttp, final HttpRxListener listener, final int sort) {
try {
// if (!NetworkUtils.isNetAvailable(activity)) {
// Toast.makeText(activity, "请检查网络", Toast.LENGTH_SHORT).show();
// return null;
// }
final Subscription subscription = postHttp.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<T>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
try {
e.printStackTrace();
// Logger.e("jrq", "onError" + e);
// Toast.makeText(MyApp.context, "获取数据失败,请稍后重试",
listener.httpResponse(null, false, sort);
} catch (Exception a) {
a.printStackTrace();
}
e.printStackTrace();
}
@Override
public void onNext(T t) {
try {
listener.httpResponse(t, true, sort);
} catch (Exception a) {
a.printStackTrace();
}
}
});
return subscription;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)