更新 HeartbeatWorker.java
parent
38e391ff46
commit
e16a598617
|
|
@ -8,6 +8,7 @@ import android.os.Handler;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonSyntaxException;
|
||||||
import com.nbee.echolink.R;
|
import com.nbee.echolink.R;
|
||||||
import com.nbee.echolink.model.DeviceInfo;
|
import com.nbee.echolink.model.DeviceInfo;
|
||||||
import com.nbee.echolink.response.ApiResponse;
|
import com.nbee.echolink.response.ApiResponse;
|
||||||
|
|
@ -47,25 +48,39 @@ public class HeartbeatWorker extends Worker {
|
||||||
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 " + heartBeatURL);
|
Timber.e(e, "请求 " + heartBeatURL + "失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
|
public void onResponse(okhttp3.Call call, okhttp3.Response response) {
|
||||||
|
try {
|
||||||
if (!response.isSuccessful()) {
|
if (!response.isSuccessful()) {
|
||||||
// 在这里处理响应错误
|
// 在这里处理响应错误
|
||||||
Timber.d("Request to " + heartBeatURL + " returned error: " + response.code() + ", " + response.message());
|
Timber.d("Request to " + heartBeatURL + " returned error: " + response.code() + ", " + response.message());
|
||||||
throw new IOException("Unexpected code " + response);
|
} 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")) {
|
||||||
Gson gson1 = new Gson();
|
Timber.d("请求返回状态码错误,返回内容: " + apiResponse);
|
||||||
ApiResponse apiResponse = gson1.fromJson(response.body().string(), ApiResponse.class);
|
}
|
||||||
if (apiResponse.getCode().equals(0)){
|
}
|
||||||
Timber.d("Received response from " + heartBeatURL + ": " + apiResponse.getCode() + " - " + apiResponse.getMsg());
|
} 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){
|
private <T>Request buildRequest(String apiUrl){
|
||||||
SN = context.getResources().getString(R.string.SN);
|
SN = context.getResources().getString(R.string.SN);
|
||||||
accessToken = context.getResources().getString(R.string.access_token);
|
accessToken = context.getResources().getString(R.string.access_token);
|
||||||
|
|
@ -73,7 +88,7 @@ public class HeartbeatWorker extends Worker {
|
||||||
deviceInfo.setDeviceBrand(DeviceInfoUtils.getDeviceBrand());
|
deviceInfo.setDeviceBrand(DeviceInfoUtils.getDeviceBrand());
|
||||||
deviceInfo.setDeviceModel(DeviceInfoUtils.getDeviceModel());
|
deviceInfo.setDeviceModel(DeviceInfoUtils.getDeviceModel());
|
||||||
deviceInfo.setAndroidVersion("Android " + DeviceInfoUtils.getDeviceAndroidVersion());
|
deviceInfo.setAndroidVersion("Android " + DeviceInfoUtils.getDeviceAndroidVersion());
|
||||||
deviceInfo.setSN(SN);
|
deviceInfo.setSn(SN);
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
// 将对象转换为JSON字符串
|
// 将对象转换为JSON字符串
|
||||||
String json = gson.toJson(deviceInfo);
|
String json = gson.toJson(deviceInfo);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue