Giter Club home page Giter Club logo

Comments (7)

kongzue avatar kongzue commented on June 15, 2024

from baseokhttpv3.

1432886945 avatar 1432886945 commented on June 15, 2024

这是我对你的网络做的封装

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.

kongzue avatar kongzue commented on June 15, 2024

很感谢,BaseOkHttp的主旨就在于简化很多处理操作以达到更高效的开发,可能对一些细节处理并不完善,我也会继续维护并完善它的功能和设计。另外您可以Fork一个分支到您的Github作出修改。

from baseokhttpv3.

1432886945 avatar 1432886945 commented on June 15, 2024

好的,我有空的话会把我修改的代码发上去,最好你能出套kotlin的,因为我都是用kotlin写的

from baseokhttpv3.

kongzue avatar kongzue commented on June 15, 2024

这个实在是抱歉,因为目前我手头项目实在太多以至于暂时未能接触kotlin的开发,目前只能用Java实现,不会有空我会学习kotlin的,感谢你的建议

from baseokhttpv3.

1432886945 avatar 1432886945 commented on June 15, 2024

嗯好的

from baseokhttpv3.

1432886945 avatar 1432886945 commented on June 15, 2024

你qq号多少?.我把我封装的demo发你,你看看,行的话,发布到你现在的demo上吧,懒得建项目了

from baseokhttpv3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.