first commit
This commit is contained in:
95
app/src/main/java/com/nbee/echolink/utils/NetworkUtil.java
Normal file
95
app/src/main/java/com/nbee/echolink/utils/NetworkUtil.java
Normal file
@@ -0,0 +1,95 @@
|
||||
package com.nbee.echolink.utils;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
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.response.ApiResponse;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
|
||||
public class NetworkUtil {
|
||||
private final String TAG = "NetworkUtil";
|
||||
private String callApiUrl;
|
||||
private String smsApiUrl;
|
||||
private String accessToken;
|
||||
private Handler handler = new Handler();
|
||||
private static final int MAX_RETRIES = 3;
|
||||
private static final long RETRY_DELAY_MS = 2000; // 重试延迟,例如2秒
|
||||
|
||||
// API URL
|
||||
public NetworkUtil(Context context) {
|
||||
callApiUrl = context.getResources().getString(R.string.call_api_url);
|
||||
smsApiUrl = context.getResources().getString(R.string.message_api_url);
|
||||
accessToken = context.getResources().getString(R.string.access_token);
|
||||
}
|
||||
|
||||
public <T> void postRequest(T dataObject) {
|
||||
if (dataObject instanceof CallInfo) {
|
||||
postRequestWithRetry(dataObject, 0,callApiUrl);
|
||||
} else if (dataObject instanceof SMSInfo) {
|
||||
postRequestWithRetry(dataObject, 0,smsApiUrl);
|
||||
}
|
||||
}
|
||||
|
||||
private <T> void postRequestWithRetry(T dataObject, int retryCount,String apiUrl) {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
// ... 构建请求 ...
|
||||
Request request = buildRequest(dataObject,apiUrl);
|
||||
|
||||
client.newCall(request).enqueue(new okhttp3.Callback() {
|
||||
@Override
|
||||
public void onFailure(okhttp3.Call call, IOException e) {
|
||||
if (retryCount < MAX_RETRIES) {
|
||||
handler.postDelayed(() -> postRequestWithRetry(dataObject, retryCount + 1,apiUrl), RETRY_DELAY_MS);
|
||||
} else {
|
||||
Log.e(TAG, "onFailure: Failed after " + MAX_RETRIES + " attempts", e);
|
||||
// 超出重试次数,处理失败情况
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
|
||||
if (!response.isSuccessful()) {
|
||||
// 在这里处理响应错误
|
||||
Log.d("onResponse", response.message());
|
||||
throw new IOException("Unexpected code " + response);
|
||||
}
|
||||
// ... 处理响应 ...
|
||||
Gson gson1 = new Gson();
|
||||
ApiResponse apiResponse = gson1.fromJson(response.body().string(), ApiResponse.class);
|
||||
if (apiResponse.getCode().equals(0)){
|
||||
Log.i(TAG, "onResponse ResponseCode: " + apiResponse.getCode() + "ResponseMessage" + apiResponse.getMsg());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private <T>Request buildRequest(T dataObject,String apiUrl){
|
||||
Gson gson = new Gson();
|
||||
// 将对象转换为JSON字符串
|
||||
String json = gson.toJson(dataObject);
|
||||
|
||||
// 创建请求体
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user