创建 HeartbeatWorker.java
parent
c0548705a2
commit
030e8250e6
|
|
@ -0,0 +1,92 @@
|
||||||
|
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.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;
|
||||||
|
|
||||||
|
|
||||||
|
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() {
|
||||||
|
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.d("Request to " + heartBeatURL);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
|
||||||
|
if (!response.isSuccessful()) {
|
||||||
|
// 在这里处理响应错误
|
||||||
|
Timber.d("Request to " + heartBeatURL + " 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);
|
||||||
|
if (apiResponse.getCode().equals(0)){
|
||||||
|
Timber.d("Received response from " + heartBeatURL + ": " + apiResponse.getCode() + " - " + apiResponse.getMsg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T>Request buildRequest(String apiUrl){
|
||||||
|
accessToken = context.getResources().getString(R.string.access_token);
|
||||||
|
DeviceInfo deviceInfo = new DeviceInfo();
|
||||||
|
deviceInfo.setDeviceBrand(DeviceInfoUtils.getDeviceBrand());
|
||||||
|
deviceInfo.setDeviceModel(DeviceInfoUtils.getDeviceModel());
|
||||||
|
deviceInfo.setAndroidVersion(DeviceInfoUtils.getDeviceAndroidVersion());
|
||||||
|
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