Closed 1432886945 closed 5 years ago
这是我对你的网络做的封装
import android.app.Activity import baseokhttp3.MediaType import com.google.gson.Gson import com.hisan.base.network.model.DataModel import com.hisan.base.network.model.callBack.DataCallBack import com.hisan.base.network.model.callBack.ErrorCallBack import com.hisan.base.ui.BaseApplication import com.hisan.base.utils.EasyLog import com.hisan.base.utils.SPUtils import com.hisan.base.utils.gson.gsonUtils import com.hjq.toast.ToastUtils import com.kongzue.baseokhttp.HttpRequest import com.kongzue.baseokhttp.exceptions.TimeOutException import com.kongzue.baseokhttp.util.BaseOkHttp import com.kongzue.baseokhttp.util.HttpParams import org.greenrobot.eventbus.EventBus import org.jsoup.Jsoup import java.io.IOException
/**
自定义网络请求库 */ class OkHttpUtils {
//单利 companion object { val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { OkHttpUtils() } }
/*
//返回数据 private fun returnData(response: String?, e: Exception?, dataCallBack: DataCallBack?, errorCallBack: ErrorCallBack?) { diaLogUtils.instance.dismissDialog() if (response != null) { if (JsonUtils.isBadJson(response)) { EasyLog.DEFAULT.v(response) val model = gsonUtils.gsonToBean(response, DataModel::class.java) if (dataIsNull(model)) { dataCallBack?.onSuccessData(gsonUtils.gsonString(model?.data)) } else { if (errorCallBack == null) { showToast(model?.msg) } else { errorCallBack.onErrorCode(model?.error_code, model?.msg) } } } else { dataCallBack?.onSuccessData(null) //解析接口错误返回的html val doc = Jsoup.parse(response) //获取字符串的数据 when (JsonUtils.extractStr(doc.title())) { 404 -> { showToast("接口地址不存在") } 500 -> { showToast("api地址异常") val doc = Jsoup.parse(response) val elements = doc.select("div.exception") EasyLog.DEFAULT.v(elements) } else -> { EasyLog.DEFAULT.v(JsonUtils.extractStr(doc.title())) } } } } if (e != null) { when (e) { //请求超时 is TimeOutException -> { showToast("请求超时") dataCallBack?.onSuccessData(null) } //数据异常 is IOException -> { EasyLog.DEFAULT.v("数据异常") dataCallBack?.onSuccessData(null) } //未知异常 else -> { EasyLog.DEFAULT.v("未知异常:" + e.toString()) } } }
}
/**
private fun showToast(msg: String?) { ToastUtils.show(msg) }
很感谢,BaseOkHttp的主旨就在于简化很多处理操作以达到更高效的开发,可能对一些细节处理并不完善,我也会继续维护并完善它的功能和设计。另外您可以Fork一个分支到您的Github作出修改。
好的,我有空的话会把我修改的代码发上去,最好你能出套kotlin的,因为我都是用kotlin写的
这个实在是抱歉,因为目前我手头项目实在太多以至于暂时未能接触kotlin的开发,目前只能用Java实现,不会有空我会学习kotlin的,感谢你的建议
嗯好的
你qq号多少?.我把我封装的demo发你,你看看,行的话,发布到你现在的demo上吧,懒得建项目了
如果返回error不为null则接口错误,具体错误请根据error类型判断
https://maas.mail.163.com/dashi-web-extend/html/proSignature.html?ftlId=1&name=myzcxhh&uid=myzcxhh%40live.cn&iconUrl=http%3A%2F%2Fmail-online.nosdn.127.net%2F58110a727ef45bedcdc890795902a8d2.jpg&items=%5B%22%E9%82%AE%E7%AE%B1%EF%BC%9Amyzcxhh%40live.cn%22%5D [http://mail-online.nosdn.127.net/58110a727ef45bedcdc890795902a8d2.jpg] myzcxhh
邮箱:myzcxhh@live.cn
Signature is customized by Netease Mail Masterhttps://mail.163.com/dashi/dlpro.html?from=mail88
在2019年01月25日 14:05,1432886945mailto:notifications@github.com 写道:
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://apc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fkongzue%2FBaseOkHttpV3%2Fissues%2F2&data=02%7C01%7C%7C735c871e1b07494fc8b708d6828b25bf%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636839931470312321&sdata=Lb7xzIuiEpSRpVDuq7YlddG8GL%2F64ui1AF9lhDwzSzo%3D&reserved=0, or mute the threadhttps://apc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAJpZH5V__a_c_iPruaGcd_tQPhabJfZmks5vGp64gaJpZM4aSQuX&data=02%7C01%7C%7C735c871e1b07494fc8b708d6828b25bf%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636839931470312321&sdata=RLXVO86Yv%2FSx5pHN2%2FG2eueJPzETbeh49mB7p6OsEtE%3D&reserved=0.