feat: 无网络检测
This commit is contained in:
parent
b9d7b4348a
commit
055e637681
|
|
@ -11,7 +11,7 @@ object Method {
|
|||
}
|
||||
|
||||
object Error {
|
||||
const val ERROR_NET = 10
|
||||
const val ERROR_NETWORK = 10
|
||||
const val ERROR_UNKNOWN = -1
|
||||
const val ERROR_PARSE = -2
|
||||
const val ERROR_NOT_LOGGED_IN = 11
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import com.huanchengfly.tieba.post.api.retrofit.converter.gson.GsonConverterFact
|
|||
import com.huanchengfly.tieba.post.api.retrofit.interceptors.AddWebCookieInterceptor
|
||||
import com.huanchengfly.tieba.post.api.retrofit.interceptors.CommonHeaderInterceptor
|
||||
import com.huanchengfly.tieba.post.api.retrofit.interceptors.CommonParamInterceptor
|
||||
import com.huanchengfly.tieba.post.api.retrofit.interceptors.ConnectivityInterceptor
|
||||
import com.huanchengfly.tieba.post.api.retrofit.interceptors.CookieInterceptor
|
||||
import com.huanchengfly.tieba.post.api.retrofit.interceptors.DropInterceptor
|
||||
import com.huanchengfly.tieba.post.api.retrofit.interceptors.FailureResponseInterceptor
|
||||
|
|
@ -224,6 +225,7 @@ object RetrofitTiebaApi {
|
|||
addInterceptor(FailureResponseInterceptor)
|
||||
addInterceptor(ForceLoginInterceptor)
|
||||
addInterceptor(sortAndSignInterceptor)
|
||||
addInterceptor(ConnectivityInterceptor)
|
||||
connectionPool(connectionPool)
|
||||
}.build())
|
||||
.build()
|
||||
|
|
@ -250,6 +252,7 @@ object RetrofitTiebaApi {
|
|||
addInterceptor(ForceLoginInterceptor)
|
||||
addInterceptor(CookieInterceptor)
|
||||
addInterceptor(sortAndSignInterceptor)
|
||||
addInterceptor(ConnectivityInterceptor)
|
||||
connectionPool(connectionPool)
|
||||
}.build())
|
||||
.build()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
package com.huanchengfly.tieba.post.api.retrofit.exception
|
||||
|
||||
import com.huanchengfly.tieba.post.api.Error.ERROR_NETWORK
|
||||
|
||||
class NoConnectivityException(
|
||||
msg: String = "No internet!"
|
||||
) : TiebaLocalException(ERROR_NETWORK, msg)
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package com.huanchengfly.tieba.post.api.retrofit.interceptors
|
||||
|
||||
import com.huanchengfly.tieba.post.App
|
||||
import com.huanchengfly.tieba.post.R
|
||||
import com.huanchengfly.tieba.post.api.retrofit.exception.NoConnectivityException
|
||||
import com.huanchengfly.tieba.post.utils.isNetworkConnected
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import java.io.IOException
|
||||
import java.net.SocketTimeoutException
|
||||
|
||||
object ConnectivityInterceptor : Interceptor {
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val response = runCatching { chain.proceed(chain.request()) }
|
||||
|
||||
val exception = response.exceptionOrNull()
|
||||
|
||||
return when {
|
||||
exception is IOException && !isNetworkConnected() -> throw NoConnectivityException(
|
||||
App.INSTANCE.getString(
|
||||
R.string.no_internet_connectivity
|
||||
)
|
||||
)
|
||||
|
||||
exception is SocketTimeoutException && isNetworkConnected() -> throw NoConnectivityException(
|
||||
App.INSTANCE.getString(R.string.connectivity_timeout)
|
||||
)
|
||||
|
||||
else -> response.getOrThrow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,61 +1,54 @@
|
|||
package com.huanchengfly.tieba.post.utils;
|
||||
package com.huanchengfly.tieba.post.utils
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkCapabilities
|
||||
import android.os.Build
|
||||
import com.huanchengfly.tieba.post.App
|
||||
|
||||
public final class NetworkUtil {
|
||||
private NetworkUtil() {
|
||||
}
|
||||
|
||||
public static boolean isNetworkConnected(Context context) {
|
||||
if (context == null) {
|
||||
return false;
|
||||
}
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (mConnectivityManager == null) {
|
||||
return false;
|
||||
}
|
||||
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
|
||||
if (mNetworkInfo == null) {
|
||||
return false;
|
||||
}
|
||||
return mNetworkInfo.isConnected();
|
||||
}
|
||||
|
||||
public static boolean isWifiConnected(Context context) {
|
||||
if (context == null) {
|
||||
return false;
|
||||
}
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (mConnectivityManager == null) {
|
||||
return false;
|
||||
}
|
||||
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
|
||||
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||
|
||||
if (mWiFiNetworkInfo == null) {
|
||||
return false;
|
||||
}
|
||||
return mWiFiNetworkInfo.isConnected();
|
||||
}
|
||||
|
||||
public static boolean isMobileConnected(Context context) {
|
||||
if (context == null) {
|
||||
return false;
|
||||
}
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (mConnectivityManager == null) {
|
||||
return false;
|
||||
}
|
||||
NetworkInfo mMobileNetworkInfo = mConnectivityManager
|
||||
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
||||
if (mMobileNetworkInfo == null) {
|
||||
return false;
|
||||
}
|
||||
return mMobileNetworkInfo.isConnected();
|
||||
fun isNetworkConnected(context: Context = App.INSTANCE): Boolean {
|
||||
val connectivityManager =
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
||||
val activeNetwork = connectivityManager.activeNetworkInfo ?: return false
|
||||
return activeNetwork.isConnected
|
||||
} else {
|
||||
val network = connectivityManager.activeNetwork ?: return false
|
||||
val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
|
||||
return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||
}
|
||||
}
|
||||
|
||||
object NetworkUtil {
|
||||
fun isNetworkConnected(context: Context?): Boolean {
|
||||
if (context == null) {
|
||||
return false
|
||||
}
|
||||
val mConnectivityManager = context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val mNetworkInfo = mConnectivityManager.activeNetworkInfo ?: return false
|
||||
return mNetworkInfo.isConnected
|
||||
}
|
||||
|
||||
fun isWifiConnected(context: Context?): Boolean {
|
||||
if (context == null) {
|
||||
return false
|
||||
}
|
||||
val mConnectivityManager = context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val mWiFiNetworkInfo = mConnectivityManager
|
||||
.getNetworkInfo(ConnectivityManager.TYPE_WIFI) ?: return false
|
||||
return mWiFiNetworkInfo.isConnected
|
||||
}
|
||||
|
||||
fun isMobileConnected(context: Context?): Boolean {
|
||||
if (context == null) {
|
||||
return false
|
||||
}
|
||||
val mConnectivityManager = context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val mMobileNetworkInfo = mConnectivityManager
|
||||
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) ?: return false
|
||||
return mMobileNetworkInfo.isConnected
|
||||
}
|
||||
}
|
||||
|
|
@ -649,4 +649,12 @@
|
|||
<string name="button_login">去登录</string>
|
||||
<string name="button_go_to_explore">随便看看</string>
|
||||
<string name="title_empty_login">你还未登录</string>
|
||||
<string name="no_internet_connectivity">无互联网连接</string>
|
||||
<string name="connectivity_timeout">连接超时</string>
|
||||
<string name="title_no_internet_connectivity">网络找不到了</string>
|
||||
<string name="title_api_error">服务器有一些自己的想法</string>
|
||||
<string name="title_unknown_error">发生了一个奇怪的错误…</string>
|
||||
<string name="message_no_internet_connectivity">%s。请检查你的网络连接是否正常</string>
|
||||
<string name="message_unknown_error">哦我的老伙计,我向你保证这真的是一个奇怪的错误……</string>
|
||||
<string name="btn_reload">重新加载</string>
|
||||
</resources>
|
||||
|
|
|
|||
Loading…
Reference in New Issue