更新 NetworkUtil.java

dev
wangsiyuan 2023-12-13 18:53:23 +08:00
parent e39170ef45
commit 976bb6316b
1 changed files with 6 additions and 8 deletions

View File

@ -21,15 +21,15 @@ import timber.log.Timber;
public class NetworkUtil { public class NetworkUtil {
private final String TAG = "NetworkUtil"; private final String TAG = "NetworkUtil";
private String callApiUrl; private final String callApiUrl;
private String smsApiUrl; private final String smsApiUrl;
private String wechatApiUrl; private final String wechatApiUrl;
private String accessToken; private final String accessToken;
private Handler handler = new Handler(); private final Handler handler = new Handler();
private static final int MAX_RETRIES = 3; private static final int MAX_RETRIES = 3;
private static final long RETRY_DELAY_MS = 2000; // 重试延迟例如2秒 private static final long RETRY_DELAY_MS = 2000; // 重试延迟例如2秒
// OkHttpClient的单例 // OkHttpClient的单例
private static OkHttpClient client = new OkHttpClient(); private static final OkHttpClient client = new OkHttpClient();
// API URL // API URL
public NetworkUtil(Context context) { public NetworkUtil(Context context) {
@ -51,7 +51,6 @@ public class NetworkUtil {
} }
private <T> void postRequestWithRetry(T dataObject, int retryCount,String apiUrl) { private <T> void postRequestWithRetry(T dataObject, int retryCount,String apiUrl) {
OkHttpClient client = new OkHttpClient();
// ... 构建请求 ... // ... 构建请求 ...
Request request = buildRequest(dataObject,apiUrl); Request request = buildRequest(dataObject,apiUrl);
Timber.d("Sending request to " + apiUrl + " (Retry count: " + retryCount + ")"); Timber.d("Sending request to " + apiUrl + " (Retry count: " + retryCount + ")");
@ -63,7 +62,6 @@ public class NetworkUtil {
if (retryCount < MAX_RETRIES) { if (retryCount < MAX_RETRIES) {
handler.postDelayed(() -> postRequestWithRetry(dataObject, retryCount + 1,apiUrl), RETRY_DELAY_MS); handler.postDelayed(() -> postRequestWithRetry(dataObject, retryCount + 1,apiUrl), RETRY_DELAY_MS);
} else { } else {
Log.e(TAG, "onFailure: Failed after " + MAX_RETRIES + " attempts", e);
Timber.e("onFailure: Failed after " + MAX_RETRIES + " attempts" + e); Timber.e("onFailure: Failed after " + MAX_RETRIES + " attempts" + e);
// 超出重试次数,处理失败情况 // 超出重试次数,处理失败情况
} }