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) {
public static boolean isNetworkConnected(Context context) { val activeNetwork = connectivityManager.activeNetworkInfo ?: return false
if (context == null) { return activeNetwork.isConnected
return false; } else {
} val network = connectivityManager.activeNetwork ?: return false
ConnectivityManager mConnectivityManager = (ConnectivityManager) context val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
.getSystemService(Context.CONNECTIVITY_SERVICE); return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
if (mConnectivityManager == null) { }
return false; }
}
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); object NetworkUtil {
if (mNetworkInfo == null) { fun isNetworkConnected(context: Context?): Boolean {
return false; if (context == null) {
} return false
return mNetworkInfo.isConnected(); }
} val mConnectivityManager = context
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
public static boolean isWifiConnected(Context context) { val mNetworkInfo = mConnectivityManager.activeNetworkInfo ?: return false
if (context == null) { return mNetworkInfo.isConnected
return false; }
}
ConnectivityManager mConnectivityManager = (ConnectivityManager) context fun isWifiConnected(context: Context?): Boolean {
.getSystemService(Context.CONNECTIVITY_SERVICE); if (context == null) {
if (mConnectivityManager == null) { return false
return false; }
} val mConnectivityManager = context
NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI); val mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI) ?: return false
if (mWiFiNetworkInfo == null) { return mWiFiNetworkInfo.isConnected
return false; }
}
return mWiFiNetworkInfo.isConnected(); fun isMobileConnected(context: Context?): Boolean {
} if (context == null) {
return false
public static boolean isMobileConnected(Context context) { }
if (context == null) { val mConnectivityManager = context
return false; .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
} val mMobileNetworkInfo = mConnectivityManager
ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getNetworkInfo(ConnectivityManager.TYPE_MOBILE) ?: return false
.getSystemService(Context.CONNECTIVITY_SERVICE); return mMobileNetworkInfo.isConnected
if (mConnectivityManager == null) {
return false;
}
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>