Comments (7)
from baseokhttpv3.
这是我对你的网络做的封装
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()
}
}/**
*- @param url 链接
- @param httpParams 请求参数
- @param request 请求方式 BaseOkHttp.GET_REQUEST、BaseOkHttp.POST_REQUEST、BaseOkHttp.PUT_REQUEST、BaseOkHttp.DELETE_REQUEST
- @param isShow 是否显示加载loading
- @param dataCallBack 请求成功回调
- @param errorCallBack 请求失败回调,默认不重写,使用默认的错误弹窗
*/
fun getData(activity: Activity, url: String, dataCallBack: DataCallBack, parameter: HttpParams? = null, request: Int? = BaseOkHttp.GET_REQUEST, isShow: Boolean? = false, errorCallBack: ErrorCallBack? = null) {
if (isShow!!) diaLogUtils.instance.showDialog(activity)
val token = SPUtils.getInstance().getString("token")
token?.takeIf { token != "" }.apply {
BaseOkHttp.overallHeader = HttpParams()
.add("token", token)
}
parameter?.let {
if (parameter.keys.contains("page") && !parameter.keys.contains("size")) parameter.add("size", 50)
if (!parameter.keys.contains("lon")) {
BaseApplication.lat?.let {
parameter.add("latitude", BaseApplication.lat!!)
}
}
if (!parameter.keys.contains("lon")) {
BaseApplication.log?.let {
parameter.add("longitude", BaseApplication.log!!)
}
}
BaseApplication.city_id?.let {
if (!parameter.keys.contains("city_id")) parameter.add("city_id", BaseApplication.city_id)
}
}
HttpRequest.build(activity, url)
.setParameter(parameter)
.setMediaType(MediaType.parse("application/jpg"))
.setResponseListener { response, error ->
returnData(response, error, dataCallBack, errorCallBack)
}.doRequest(request!!)
}
}
//返回数据
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 dataIsNull(data: DataModel?): Boolean {
val errorCode = data?.error_code
if (errorCode != 0) {
when (errorCode) {
10000 -> EasyLog.DEFAULT.v("数据请求验证失败")
10001 -> EasyLog.DEFAULT.v("无效的参数")
10002 -> EasyLog.DEFAULT.v("请求参数无效,请重新输入")
10003 -> EasyLog.DEFAULT.v("请求参数不存在")
20001 -> showToast("登录失败,请稍后再试")
20002 -> showToast("登录失败,请检查用户名和密码")
20003 -> showToast("登录失败,账户异常,请联系管理员")
20004 -> EasyLog.DEFAULT.v("等待扫描登录")
20005 -> EasyLog.DEFAULT.v("AUTH登录类型错误")
20006 -> showToast("当前账号未注册")
20007 -> showToast("分享码错误")
20008 -> showToast("账号已存在")
20009 -> showToast("注册失败")
20010 -> showToast("请先绑定手机号")
40001 -> EventBus.getDefault().post("当前用户已退出,请重新登入")
40002 -> {
EasyLog.DEFAULT.v("服务器缓存异常,一般为登录")
showToast("服务器异常,请稍后再试")
}
40004 -> {
EasyLog.DEFAULT.v("客户ip进入黑名单")
showToast("账号异常无法使用,请联系管理员")
}
40005, 40006 -> showToast("当前用户无操作权限")
50001 -> showToast("文件内容不能为空")
50002 -> showToast("上传出错了")
60001 -> {
showToast("当前店铺被删除,无法访问")
}
61001 -> {
showToast("当前商品被删除,无法访问")
}
90001 -> {
EasyLog.DEFAULT.v("数据已存在")
showToast("当前数据已存在,请勿重复添加")
}
90002 -> {
EasyLog.DEFAULT.v("缺乏参数,操作失败")
}
90003 -> {
showToast("定位失败")
}
}
return false
}
return true
}
private fun showToast(msg: String?) {
ToastUtils.show(msg)
}
from baseokhttpv3.
很感谢,BaseOkHttp的主旨就在于简化很多处理操作以达到更高效的开发,可能对一些细节处理并不完善,我也会继续维护并完善它的功能和设计。另外您可以Fork一个分支到您的Github作出修改。
from baseokhttpv3.
好的,我有空的话会把我修改的代码发上去,最好你能出套kotlin的,因为我都是用kotlin写的
from baseokhttpv3.
这个实在是抱歉,因为目前我手头项目实在太多以至于暂时未能接触kotlin的开发,目前只能用Java实现,不会有空我会学习kotlin的,感谢你的建议
from baseokhttpv3.
嗯好的
from baseokhttpv3.
你qq号多少?.我把我封装的demo发你,你看看,行的话,发布到你现在的demo上吧,懒得建项目了
from baseokhttpv3.
Related Issues (20)
- 对于post 请求 Query Params方式传参?怎么传? HOT 5
- 文件POST上传 HOT 3
- 文件下载建议
- 建议增加 请求错误码统一处理 HOT 1
- 文件上传 大小超过6m提示超时 HOT 18
- kotlin冲突了,怎么排除内部依赖的kotlin-stdlib这个依赖 HOT 2
- 关于文件上传,设置全局请求拦截
- 关于文件下载
- 你好 请问如何返回字节应该怎么处理呢
- 你好,选择了某些文件后点击上传按钮就没反应 HOT 1
- 你好 问下 我mvvm模式 viewmodel里面不适合传入context 请问操作 HOT 3
- 跳过SSL认证
- 文件下载地址若失效,会走onDownloadSuccess回调
- 上传问题 HOT 1
- 内存泄漏
- 本人小白,不太懂如何解析response,希望大神能教一下。 HOT 2
- websocket HOT 6
- https
- 怎么跳过SSL证书验证,build.skipSSLCheck();这个方法无效
- websocket问题 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from baseokhttpv3.