refactor(app): 优化设备信息上传逻辑

- 重构了 TaskUtil 类中的设备信息上传方法- 使用 Builder 模式构建 HttpUrl,提高了代码的可读性和可维护性
- 增加了对 packageName 参数的空值检查,避免不必要的查询参数
- 移除了冗余的异常抛出,改为直接返回,简化了错误处理逻辑
This commit is contained in:
yjj38 2025-07-07 18:30:32 +08:00
parent 530e2d6af2
commit b003864b97
3 changed files with 64 additions and 9 deletions

View File

@ -1,3 +1,57 @@
POST http://47.238.96.231:8112/device_info_upload?id=FyZqWrStUvOpKlMn&taskId=d2fc8ffb-63a7-4c03-ae10-2eeb9ce8c989&deviceIp={"timezone":"America\/Los_Angeles","ip":"74.82.228.84","organization":"AS25764 NWF-IFIBER-ASN-2","asn":25764,"area_code":"0","organization_name":"NWF-IFIBER-ASN-2","country_code":"US","country_code3":"USA","continent_code":"NA","accuracy":50,"region":"Washington","latitude":"47.1285","longitude":"-119.2883","city":"Moses Lake","country":"United States"}
Content-Type: application/json; charset=utf-8
Content-Length: 114
User-Agent: IntelliJ HTTP Client/Android Studio Narwhal | 2025.1.1
Accept-Encoding: br, deflate, gzip, x-gzip
Accept: */*
{
"bigoDeviceObject": BIGO_DEVICE_OBJECT,
"afDeviceObject": AF_DEVICE_OBJECT,
"other": DEVICE_INFO
}
<> 2025-07-07T182436.200.json
###
POST http://47.238.96.231:8112/device_info_upload?id=FyZqWrStUvOpKlMn&taskId=d2fc8ffb-63a7-4c03-ae10-2eeb9ce8c989&deviceIp={"timezone":"America\/Los_Angeles","ip":"74.82.228.84","organization":"AS25764 NWF-IFIBER-ASN-2","asn":25764,"area_code":"0","organization_name":"NWF-IFIBER-ASN-2","country_code":"US","country_code3":"USA","continent_code":"NA","accuracy":50,"region":"Washington","latitude":"47.1285","longitude":"-119.2883","city":"Moses Lake","country":"United States"}
Content-Type: application/json; charset=utf-8
Content-Length: 114
User-Agent: IntelliJ HTTP Client/Android Studio Narwhal | 2025.1.1
Accept-Encoding: br, deflate, gzip, x-gzip
Accept: */*
{
"bigoDeviceObject": BIGO_DEVICE_OBJECT,
"afDeviceObject": AF_DEVICE_OBJECT,
"other": DEVICE_INFO
}
<> 2025-07-07T181724.200.json
###
POST http://47.238.96.231:8112/device_info_upload?id=FyZqWrStUvOpKlMn&taskId=d2fc8ffb-63a7-4c03-ae10-2eeb9ce8c989&deviceIp={"timezone":"America\/Los_Angeles","ip":"74.82.228.84","organization":"AS25764 NWF-IFIBER-ASN-2","asn":25764,"area_code":"0","organization_name":"NWF-IFIBER-ASN-2","country_code":"US","country_code3":"USA","continent_code":"NA","accuracy":50,"region":"Washington","latitude":"47.1285","longitude":"-119.2883","city":"Moses Lake","country":"United States"}
Content-Type: application/json; charset=utf-8
Content-Length: 114
User-Agent: IntelliJ HTTP Client/Android Studio Narwhal | 2025.1.1
Accept-Encoding: br, deflate, gzip, x-gzip
Accept: */*
{
"bigoDeviceObject": BIGO_DEVICE_OBJECT,
"afDeviceObject": AF_DEVICE_OBJECT,
"other": DEVICE_INFO
}
<> 2025-07-07T181305.200.json
###
GET http://47.238.96.231:8112/get_package_info?androidId=b3d893cf9de3a85a
User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2025.1.2
Accept-Encoding: br, deflate, gzip, x-gzip

View File

@ -1,6 +1,7 @@
package com.example.studyapp.task;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.example.studyapp.utils.FileUtils;
@ -20,6 +21,7 @@ import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import okhttp3.HttpUrl;
import okhttp3.HttpUrl.Builder;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
@ -77,19 +79,18 @@ public class TaskUtil {
Log.d("TaskUtil", "Request payload: " + jsonRequestBody);
if (packageName == null){
packageName = "";
}
HttpUrl url = HttpUrl.parse(BASE_URL)
Builder urlBuilder = HttpUrl.parse(BASE_URL)
.newBuilder()
.addPathSegment("device_info_upload")
.addQueryParameter("id", androidId)
.addQueryParameter("taskId", taskId)
.addQueryParameter("packageName", packageName)
.addQueryParameter("deviceIp",ipInfo)
.build();
.addQueryParameter("deviceIp",ipInfo);
if (!TextUtils.isEmpty(packageName)){
urlBuilder.addQueryParameter("packageName", packageName);
}
HttpUrl url = urlBuilder.build();
Log.d("TaskUtil", "Request URL: " + url.toString());
RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), jsonRequestBody);
@ -188,7 +189,7 @@ public class TaskUtil {
if (packAge == null || packAge.isEmpty()) {
LogFileUtil.logAndWrite(android.util.Log.ERROR, "TaskUtil", "Package name is null or empty", null);
throw new IllegalArgumentException("Package name cannot be null or empty");
return;
}
if (context == null) {