Compare commits
2 Commits
79a246cbd6
...
e16a598617
| Author | SHA1 | Date |
|---|---|---|
|
|
e16a598617 | |
|
|
38e391ff46 |
|
|
@ -4,16 +4,16 @@ public class DeviceInfo {
|
|||
private String deviceBrand;
|
||||
private String deviceModel;
|
||||
private String androidVersion;
|
||||
private String SN;
|
||||
private String sn;
|
||||
|
||||
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.deviceModel = deviceModel;
|
||||
this.androidVersion = androidVersion;
|
||||
this.SN = SN;
|
||||
this.sn = sn;
|
||||
}
|
||||
|
||||
public String getDeviceBrand() {
|
||||
|
|
@ -40,12 +40,12 @@ public class DeviceInfo {
|
|||
this.androidVersion = androidVersion;
|
||||
}
|
||||
|
||||
public String getSN() {
|
||||
return SN;
|
||||
public String getSn() {
|
||||
return sn;
|
||||
}
|
||||
|
||||
public void setSN(String SN) {
|
||||
this.SN = SN;
|
||||
public void setSn(String sn) {
|
||||
this.sn = sn;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -54,7 +54,7 @@ public class DeviceInfo {
|
|||
"deviceBrand='" + deviceBrand + '\'' +
|
||||
", deviceModel='" + deviceModel + '\'' +
|
||||
", androidVersion='" + androidVersion + '\'' +
|
||||
", SN='" + SN + '\'' +
|
||||
", SN='" + sn + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ 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;
|
||||
|
|
@ -47,25 +48,39 @@ public class HeartbeatWorker extends Worker {
|
|||
client.newCall(request).enqueue(new okhttp3.Callback() {
|
||||
@Override
|
||||
public void onFailure(okhttp3.Call call, IOException e) {
|
||||
Timber.d("Request to " + heartBeatURL);
|
||||
Timber.e(e, "请求 " + 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());
|
||||
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);
|
||||
|
|
@ -73,7 +88,7 @@ public class HeartbeatWorker extends Worker {
|
|||
deviceInfo.setDeviceBrand(DeviceInfoUtils.getDeviceBrand());
|
||||
deviceInfo.setDeviceModel(DeviceInfoUtils.getDeviceModel());
|
||||
deviceInfo.setAndroidVersion("Android " + DeviceInfoUtils.getDeviceAndroidVersion());
|
||||
deviceInfo.setSN(SN);
|
||||
deviceInfo.setSn(SN);
|
||||
Gson gson = new Gson();
|
||||
// 将对象转换为JSON字符串
|
||||
String json = gson.toJson(deviceInfo);
|
||||
|
|
|
|||
Loading…
Reference in New Issue