Compare commits

...

2 Commits

Author SHA1 Message Date
e16a598617 更新 HeartbeatWorker.java 2023-12-09 15:31:28 +08:00
38e391ff46 更新 DeviceInfo.java 2023-12-09 15:31:26 +08:00
2 changed files with 36 additions and 21 deletions

View File

@@ -4,16 +4,16 @@ public class DeviceInfo {
private String deviceBrand; private String deviceBrand;
private String deviceModel; private String deviceModel;
private String androidVersion; private String androidVersion;
private String SN; private String sn;
public DeviceInfo() { public DeviceInfo() {
} }
public DeviceInfo(String deviceBrand, String deviceModel, String androidVersion,String SN) { public DeviceInfo(String deviceBrand, String deviceModel, String androidVersion,String sn) {
this.deviceBrand = deviceBrand; this.deviceBrand = deviceBrand;
this.deviceModel = deviceModel; this.deviceModel = deviceModel;
this.androidVersion = androidVersion; this.androidVersion = androidVersion;
this.SN = SN; this.sn = sn;
} }
public String getDeviceBrand() { public String getDeviceBrand() {
@@ -40,12 +40,12 @@ public class DeviceInfo {
this.androidVersion = androidVersion; this.androidVersion = androidVersion;
} }
public String getSN() { public String getSn() {
return SN; return sn;
} }
public void setSN(String SN) { public void setSn(String sn) {
this.SN = SN; this.sn = sn;
} }
@Override @Override
@@ -54,7 +54,7 @@ public class DeviceInfo {
"deviceBrand='" + deviceBrand + '\'' + "deviceBrand='" + deviceBrand + '\'' +
", deviceModel='" + deviceModel + '\'' + ", deviceModel='" + deviceModel + '\'' +
", androidVersion='" + androidVersion + '\'' + ", androidVersion='" + androidVersion + '\'' +
", SN='" + SN + '\'' + ", SN='" + sn + '\'' +
'}'; '}';
} }
} }

View File

@@ -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) {
if (!response.isSuccessful()) { try {
// 在这里处理响应错误 if (!response.isSuccessful()) {
Timber.d("Request to " + heartBeatURL + " returned error: " + response.code() + ", " + response.message()); // 在这里处理响应错误
throw new IOException("Unexpected code " + response); Timber.d("Request to " + heartBeatURL + " returned error: " + response.code() + ", " + response.message());
} } else {
// ... 处理响应 ... // 处理成功的响应
Gson gson1 = new Gson(); String responseBody = response.body().string(); // 获取响应体
ApiResponse apiResponse = gson1.fromJson(response.body().string(), ApiResponse.class); Gson gson = new Gson();
if (apiResponse.getCode().equals(0)){ ApiResponse apiResponse = gson.fromJson(responseBody, ApiResponse.class);
Timber.d("Received response from " + heartBeatURL + ": " + apiResponse.getCode() + " - " + apiResponse.getMsg()); 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){ 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);