feat: 无网络检测

This commit is contained in:
HuanCheng65 2023-03-11 23:15:55 +08:00
parent b9d7b4348a
commit 055e637681
No known key found for this signature in database
GPG Key ID: E9031EF91A805148
6 changed files with 93 additions and 50 deletions

View File

@ -11,7 +11,7 @@ object Method {
} }
object Error { object Error {
const val ERROR_NET = 10 const val ERROR_NETWORK = 10
const val ERROR_UNKNOWN = -1 const val ERROR_UNKNOWN = -1
const val ERROR_PARSE = -2 const val ERROR_PARSE = -2
const val ERROR_NOT_LOGGED_IN = 11 const val ERROR_NOT_LOGGED_IN = 11

View File

@ -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.AddWebCookieInterceptor
import com.huanchengfly.tieba.post.api.retrofit.interceptors.CommonHeaderInterceptor 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.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.CookieInterceptor
import com.huanchengfly.tieba.post.api.retrofit.interceptors.DropInterceptor import com.huanchengfly.tieba.post.api.retrofit.interceptors.DropInterceptor
import com.huanchengfly.tieba.post.api.retrofit.interceptors.FailureResponseInterceptor import com.huanchengfly.tieba.post.api.retrofit.interceptors.FailureResponseInterceptor
@ -224,6 +225,7 @@ object RetrofitTiebaApi {
addInterceptor(FailureResponseInterceptor) addInterceptor(FailureResponseInterceptor)
addInterceptor(ForceLoginInterceptor) addInterceptor(ForceLoginInterceptor)
addInterceptor(sortAndSignInterceptor) addInterceptor(sortAndSignInterceptor)
addInterceptor(ConnectivityInterceptor)
connectionPool(connectionPool) connectionPool(connectionPool)
}.build()) }.build())
.build() .build()
@ -250,6 +252,7 @@ object RetrofitTiebaApi {
addInterceptor(ForceLoginInterceptor) addInterceptor(ForceLoginInterceptor)
addInterceptor(CookieInterceptor) addInterceptor(CookieInterceptor)
addInterceptor(sortAndSignInterceptor) addInterceptor(sortAndSignInterceptor)
addInterceptor(ConnectivityInterceptor)
connectionPool(connectionPool) connectionPool(connectionPool)
}.build()) }.build())
.build() .build()

View File

@ -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)

View File

@ -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()
}
}
}

View File

@ -1,61 +1,54 @@
package com.huanchengfly.tieba.post.utils; package com.huanchengfly.tieba.post.utils
import android.content.Context; import android.content.Context
import android.net.ConnectivityManager; import android.net.ConnectivityManager
import android.net.NetworkInfo; import android.net.NetworkCapabilities
import android.os.Build
import com.huanchengfly.tieba.post.App
public final class NetworkUtil { fun isNetworkConnected(context: Context = App.INSTANCE): Boolean {
private NetworkUtil() { 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)
}
} }
public static boolean isNetworkConnected(Context context) { object NetworkUtil {
fun isNetworkConnected(context: Context?): Boolean {
if (context == null) { if (context == null) {
return false; return false
} }
ConnectivityManager mConnectivityManager = (ConnectivityManager) context val mConnectivityManager = context
.getSystemService(Context.CONNECTIVITY_SERVICE); .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (mConnectivityManager == null) { val mNetworkInfo = mConnectivityManager.activeNetworkInfo ?: return false
return false; return mNetworkInfo.isConnected
}
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo == null) {
return false;
}
return mNetworkInfo.isConnected();
} }
public static boolean isWifiConnected(Context context) { fun isWifiConnected(context: Context?): Boolean {
if (context == null) { if (context == null) {
return false; return false
} }
ConnectivityManager mConnectivityManager = (ConnectivityManager) context val mConnectivityManager = context
.getSystemService(Context.CONNECTIVITY_SERVICE); .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (mConnectivityManager == null) { val mWiFiNetworkInfo = mConnectivityManager
return false; .getNetworkInfo(ConnectivityManager.TYPE_WIFI) ?: return false
} return mWiFiNetworkInfo.isConnected
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo == null) {
return false;
}
return mWiFiNetworkInfo.isConnected();
} }
public static boolean isMobileConnected(Context context) { fun isMobileConnected(context: Context?): Boolean {
if (context == null) { if (context == null) {
return false; return false
} }
ConnectivityManager mConnectivityManager = (ConnectivityManager) context val mConnectivityManager = context
.getSystemService(Context.CONNECTIVITY_SERVICE); .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (mConnectivityManager == null) { val mMobileNetworkInfo = mConnectivityManager
return false; .getNetworkInfo(ConnectivityManager.TYPE_MOBILE) ?: return false
} return mMobileNetworkInfo.isConnected
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo == null) {
return false;
}
return mMobileNetworkInfo.isConnected();
} }
} }

View File

@ -649,4 +649,12 @@
<string name="button_login">去登录</string> <string name="button_login">去登录</string>
<string name="button_go_to_explore">随便看看</string> <string name="button_go_to_explore">随便看看</string>
<string name="title_empty_login">你还未登录</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> </resources>