1.api
object Const {
// const val BASE_URL = "http://***.***.***.***:**/auto/back/orderSale/"
const val BASE_URL = "http://gank.io/api/"
// const val BASE_URL = "http://***.***.***:***/*******/"
//get
interface ArticalApi {
@GET("xiandu/category1/{path}")
suspend fun category(
@Path("path") category: String): Artical} // form interface OrderApi {@post ("PDA/pickOrderSaleListDetail.do")
@FormUrlEncoded
suspend fun pickOrderSaleListDetail(
@Field("orderId") orderId: String,
@Field("status") status: Int
): Order
}
//json
interface StockApi {
@POST("api/basic/stockList")
suspend fun stockList(
@Body empty: UpEmpty
): Stock
}
}
Copy the code
2. Exception handling classes
object ExceptionHandle {
private const val UNAUTHORIZED = 401
private const val FORBIDDEN = 403
private const val NOT_FOUND = 404
private const val REQUEST_TIMEOUT = 408
private const val INTERNAL_SERVER_ERROR = 500
private const val BAD_GATEWAY = 502
private const val SERVICE_UNAVAILABLE = 503
private const val GATEWAY_TIMEOUT = 504
fun handle(e: Throwable): ResponeThrowable {
val ex: ResponeThrowable
return when (e) {
is HttpException -> {
ex = ResponeThrowable(e)
when (e.code()) {
UNAUTHORIZED, FORBIDDEN -> ex.message = "No access"
NOT_FOUND -> ex.message = "Can't find the server"
REQUEST_TIMEOUT -> ex.message = "Request timed out"
GATEWAY_TIMEOUT -> ex.message = "Gateway timed out"
INTERNAL_SERVER_ERROR -> ex.message = "Server error"
BAD_GATEWAY -> ex.message = "Gateway error"
SERVICE_UNAVAILABLE -> ex.message = "Server is not responding"
else -> ex.message = "Network error"
}
ex
}
is ServerException -> {
val resultException = e
ex = ResponeThrowable(resultException)
ex.message = resultException.message
ex
}
is JsonParseException, is JSONException -> {
ex = ResponeThrowable(e)
ex.message = "Parsing error"
ex
}
is ConnectException -> {
ex = ResponeThrowable(e)
ex.message = "Connection failed. Please check network."
ex
}
is SSLHandshakeException -> {
ex = ResponeThrowable(e)
ex.message = "Certificate verification failed"Ex} is SocketTimeoutException -> {// Timeout ex = ResponeThrowable(e) ex. Message ="Request timed out"
ex
}
else -> {
ex = ResponeThrowable(e)
ex.message = "Unknown error"ex } } } class ResponeThrowable(throwable: Throwable?) : Exception(throwable) { override var message: String? = null} // When a ServerException occurs, it is automatically converted to ResponeThrowable to return an internal class ServerException:RuntimeException() {
override var message: String? = null
}
}
Copy the code
Use 3.
Fun getInfo (): MutableLiveData<Artical> {// Coroutine understanding, oksuspend, and does not block the thread. Lightweight threads Jobutil. Scope. Launch {try {val artical = RFUtil. Rf. Create (Const. ArticalApi: : class. Java). The category ("hahaha")
info.value = artical
Jobutil.io("nihao")
delay(1000)
Jobutil.io("ok")
Log.i("result-->"."thread:${Thread.currentThread().name}")
} catch (e: Exception) {
error.value = ExceptionHandle.handle(e).message
}
}
return info
}
Copy the code