创建 NetworkUtils.java
parent
acf592153e
commit
a5b306a4a2
|
|
@ -0,0 +1,105 @@
|
||||||
|
package com.nbee.echolink.utils;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Handler;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.nbee.echolink.R;
|
||||||
|
import com.nbee.echolink.model.CallInfo;
|
||||||
|
import com.nbee.echolink.model.SMSInfo;
|
||||||
|
import com.nbee.echolink.model.WeChatMsg;
|
||||||
|
import com.nbee.echolink.response.ApiResponse;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
public class NetworkUtils {
|
||||||
|
private final String TAG = "NetworkUtils";
|
||||||
|
private final String callApiUrl;
|
||||||
|
private final String smsApiUrl;
|
||||||
|
private final String wechatApiUrl;
|
||||||
|
private final String accessToken;
|
||||||
|
private final Handler handler = new Handler();
|
||||||
|
private static final int MAX_RETRIES = 3;
|
||||||
|
private static final long RETRY_DELAY_MS = 2000; // 重试延迟,例如2秒
|
||||||
|
// OkHttpClient的单例
|
||||||
|
private static final OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
// API URL
|
||||||
|
public NetworkUtils(Context context) {
|
||||||
|
callApiUrl = context.getResources().getString(R.string.call_api_url);
|
||||||
|
smsApiUrl = context.getResources().getString(R.string.message_api_url);
|
||||||
|
wechatApiUrl= context.getResources().getString(R.string.send_wechat_msg_api_url);
|
||||||
|
accessToken = context.getResources().getString(R.string.access_token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> void postRequest(T dataObject) {
|
||||||
|
// 使用单例的OkHttpClient
|
||||||
|
if (dataObject instanceof CallInfo) {
|
||||||
|
postRequestWithRetry(dataObject, 0, callApiUrl);
|
||||||
|
} else if (dataObject instanceof SMSInfo) {
|
||||||
|
postRequestWithRetry(dataObject, 0, smsApiUrl);
|
||||||
|
} else if (dataObject instanceof WeChatMsg){
|
||||||
|
postRequestWithRetry(dataObject,0,wechatApiUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> void postRequestWithRetry(T dataObject, int retryCount,String apiUrl) {
|
||||||
|
// ... 构建请求 ...
|
||||||
|
Request request = buildRequest(dataObject,apiUrl);
|
||||||
|
Timber.d("Sending request to " + apiUrl + " (Retry count: " + retryCount + ")");
|
||||||
|
|
||||||
|
client.newCall(request).enqueue(new okhttp3.Callback() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(okhttp3.Call call, IOException e) {
|
||||||
|
Timber.d("Request to " + apiUrl + " failed on attempt " + retryCount+ e);
|
||||||
|
if (retryCount < MAX_RETRIES) {
|
||||||
|
handler.postDelayed(() -> postRequestWithRetry(dataObject, retryCount + 1,apiUrl), RETRY_DELAY_MS);
|
||||||
|
} else {
|
||||||
|
Timber.e("onFailure: Failed after " + MAX_RETRIES + " attempts" + e);
|
||||||
|
// 超出重试次数,处理失败情况
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
|
||||||
|
if (!response.isSuccessful()) {
|
||||||
|
// 在这里处理响应错误
|
||||||
|
Timber.d("Request to " + apiUrl + " returned error: " + response.code() + ", " + response.message());
|
||||||
|
throw new IOException("Unexpected code " + response);
|
||||||
|
}
|
||||||
|
// ... 处理响应 ...
|
||||||
|
Gson gson1 = new Gson();
|
||||||
|
ApiResponse apiResponse = gson1.fromJson(response.body().string(), ApiResponse.class);
|
||||||
|
Timber.d("ApiResponse" + apiResponse);
|
||||||
|
if (apiResponse.getCode().equals(0)){
|
||||||
|
Timber.d("Received response from " + apiUrl + ": " + apiResponse.getCode() + " - " + apiResponse.getMsg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T>Request buildRequest(T dataObject,String apiUrl){
|
||||||
|
Gson gson = new Gson();
|
||||||
|
// 将对象转换为JSON字符串
|
||||||
|
String json = gson.toJson(dataObject);
|
||||||
|
Timber.d("Building request to " + apiUrl + " with data: " + json);
|
||||||
|
// 创建请求体
|
||||||
|
MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||||
|
RequestBody body = RequestBody.create(json, JSON);
|
||||||
|
|
||||||
|
// 构建请求,并设置请求头
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(apiUrl)
|
||||||
|
.addHeader("accessToken", accessToken) // 使用加载的accessToken
|
||||||
|
.addHeader("Content-Type", "application/json")
|
||||||
|
.post(body)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue