更新 NetworkUtil.java

dev
wangsiyuan 2023-12-07 19:23:17 +08:00
parent 4bd88021a2
commit b63e99b857
1 changed files with 7 additions and 3 deletions

View File

@ -16,6 +16,7 @@ import okhttp3.MediaType;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.RequestBody; import okhttp3.RequestBody;
import timber.log.Timber;
public class NetworkUtil { public class NetworkUtil {
private final String TAG = "NetworkUtil"; private final String TAG = "NetworkUtil";
@ -45,14 +46,17 @@ public class NetworkUtil {
OkHttpClient client = new OkHttpClient(); OkHttpClient client = new OkHttpClient();
// ... 构建请求 ... // ... 构建请求 ...
Request request = buildRequest(dataObject,apiUrl); Request request = buildRequest(dataObject,apiUrl);
Timber.d("Sending request to " + apiUrl + " (Retry count: " + retryCount + ")");
client.newCall(request).enqueue(new okhttp3.Callback() { client.newCall(request).enqueue(new okhttp3.Callback() {
@Override @Override
public void onFailure(okhttp3.Call call, IOException e) { public void onFailure(okhttp3.Call call, IOException e) {
Timber.d("Request to " + apiUrl + " failed on attempt " + retryCount+ e);
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); Log.e(TAG, "onFailure: Failed after " + MAX_RETRIES + " attempts", e);
Timber.e("onFailure: Failed after " + MAX_RETRIES + " attempts" + e);
// 超出重试次数,处理失败情况 // 超出重试次数,处理失败情况
} }
} }
@ -60,14 +64,14 @@ public class NetworkUtil {
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException { public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
if (!response.isSuccessful()) { if (!response.isSuccessful()) {
// 在这里处理响应错误 // 在这里处理响应错误
Log.d("onResponse", response.message()); Timber.d("Request to " + apiUrl + " returned error: " + response.code() + ", " + response.message());
throw new IOException("Unexpected code " + response); throw new IOException("Unexpected code " + response);
} }
// ... 处理响应 ... // ... 处理响应 ...
Gson gson1 = new Gson(); Gson gson1 = new Gson();
ApiResponse apiResponse = gson1.fromJson(response.body().string(), ApiResponse.class); ApiResponse apiResponse = gson1.fromJson(response.body().string(), ApiResponse.class);
if (apiResponse.getCode().equals(0)){ if (apiResponse.getCode().equals(0)){
Log.i(TAG, "onResponse ResponseCode: " + apiResponse.getCode() + "ResponseMessage" + apiResponse.getMsg()); Timber.d("Received response from " + apiUrl + ": " + apiResponse.getCode() + " - " + apiResponse.getMsg());
} }
} }
}); });
@ -77,7 +81,7 @@ public class NetworkUtil {
Gson gson = new Gson(); Gson gson = new Gson();
// 将对象转换为JSON字符串 // 将对象转换为JSON字符串
String json = gson.toJson(dataObject); String json = gson.toJson(dataObject);
Timber.d("Building request to " + apiUrl + " with data: " + json);
// 创建请求体 // 创建请求体
MediaType JSON = MediaType.get("application/json; charset=utf-8"); MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody body = RequestBody.create(json, JSON); RequestBody body = RequestBody.create(json, JSON);