删除 HeartbeatWorker.java
parent
935ded253b
commit
cbf7f3eeea
|
|
@ -1,111 +0,0 @@
|
||||||
package com.nbee.echolink.utils;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.work.Worker;
|
|
||||||
import androidx.work.WorkerParameters;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Handler;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.JsonSyntaxException;
|
|
||||||
import com.nbee.echolink.R;
|
|
||||||
import com.nbee.echolink.model.DeviceInfo;
|
|
||||||
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 HeartbeatWorker extends Worker {
|
|
||||||
private final Context context;
|
|
||||||
private String accessToken;
|
|
||||||
private String SN;
|
|
||||||
|
|
||||||
|
|
||||||
public HeartbeatWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
|
|
||||||
super(context, workerParams);
|
|
||||||
this.context = context; // 保存上下文引用
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
public Result doWork() {
|
|
||||||
Context context = getApplicationContext();
|
|
||||||
// 在这里实现您的心跳发送逻辑
|
|
||||||
sendHeartbeatSignal();
|
|
||||||
return Result.success();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendHeartbeatSignal() {
|
|
||||||
Timber.d("开始执行发送心跳");
|
|
||||||
String heartBeatURL = context.getResources().getString(R.string.heart_beat_url); // 获取URL
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
|
||||||
Request request = buildRequest(heartBeatURL);
|
|
||||||
client.newCall(request).enqueue(new okhttp3.Callback() {
|
|
||||||
@Override
|
|
||||||
public void onFailure(okhttp3.Call call, IOException e) {
|
|
||||||
Timber.e(e, "请求 " + heartBeatURL + "失败");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResponse(okhttp3.Call call, okhttp3.Response response) {
|
|
||||||
try {
|
|
||||||
if (!response.isSuccessful()) {
|
|
||||||
// 在这里处理响应错误
|
|
||||||
Timber.d("Request to " + heartBeatURL + " returned error: " + response.code() + ", " + response.message());
|
|
||||||
} else {
|
|
||||||
// 处理成功的响应
|
|
||||||
String responseBody = response.body().string(); // 获取响应体
|
|
||||||
Gson gson = new Gson();
|
|
||||||
ApiResponse apiResponse = gson.fromJson(responseBody, ApiResponse.class);
|
|
||||||
if (apiResponse == null){
|
|
||||||
Timber.d("apiResponse is null.");
|
|
||||||
}
|
|
||||||
if (!apiResponse.getCode().equals("0")) {
|
|
||||||
Timber.d("请求返回状态码错误,返回内容: " + apiResponse);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
Timber.e(e, "IOException during handling response");
|
|
||||||
} catch (JsonSyntaxException e) {
|
|
||||||
Timber.e(e, "JsonSyntaxException during parsing response");
|
|
||||||
} finally {
|
|
||||||
response.close(); // 确保响应体被关闭
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private <T>Request buildRequest(String apiUrl){
|
|
||||||
SN = context.getResources().getString(R.string.SN);
|
|
||||||
accessToken = context.getResources().getString(R.string.access_token);
|
|
||||||
DeviceInfo deviceInfo = new DeviceInfo();
|
|
||||||
deviceInfo.setDeviceBrand(DeviceInfoUtils.getDeviceBrand());
|
|
||||||
deviceInfo.setDeviceModel(DeviceInfoUtils.getDeviceModel());
|
|
||||||
deviceInfo.setAndroidVersion("Android " + DeviceInfoUtils.getDeviceAndroidVersion());
|
|
||||||
deviceInfo.setSn(SN);
|
|
||||||
Gson gson = new Gson();
|
|
||||||
// 将对象转换为JSON字符串
|
|
||||||
String json = gson.toJson(deviceInfo);
|
|
||||||
// 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