优化这个方法:private void saveFileBrowseRecord(LayoutElementParcelable file) { // 当前的目录 String currentPath = file.desc.substring(0, file.desc.lastIndexOf(File.separator)); String basePath = mSourceRootPath; if (mTransferType == U_FTP_TO_FAB_FTP) { basePath = mSourcePath; } XLog.tag(TAG).i("mCurrentPath:" + currentPath + " basePath:" + basePath); HybridFileParcelable hybridFileParcelable = new HybridFileParcelable(currentPath, basePath, file.desc, file.permissions, file.date, file.longSize, file.isDirectory, file.getMode(), ""); if (!TextUtils.isEmpty(file.title)) { hybridFileParcelable.setName(file.title); } Map<String, Object> map = new HashMap<>(); // 文件路径全名称 map.put("dirFullName", hybridFileParcelable.getRelativePath()); // 文件服务器id map.put("fileServerId", mStoreServerId); RequestBody body = HttpClient.getRequestBody(map); TransferApi api = HttpClient.api(TransferApi.class, false); if (api == null) { XLog.tag(TAG).i("save file browse record api is null"); return; } // recordType 记录类型:默认0-最近浏览,1-收藏 api.saveFileBrowseRecord(body, 1) .compose(HttpClient.observableIoToMain()) .as(HttpClient.bindLifecycle(this)) .subscribe(new HttpDefaultObserver<HttpResult<String>>() { @Override public void start() { } @Override public void success(HttpResult<String> result) { mCollects.put(file.desc, file.desc); } @Override public void fail(HttpError e) { showWhiteFailToast(e.getCode(), e.getMessage(), null); } @Override public void finish() { } }); }
时间: 2024-02-10 22:12:08 浏览: 136
读取基于文本的数据文件.zip_Tabú_dataFile.zip
首先,可以将以下代码段:
```
HybridFileParcelable hybridFileParcelable = new HybridFileParcelable(currentPath,
basePath, file.desc, file.permissions, file.date,
file.longSize, file.isDirectory, file.getMode(), "");
if (!TextUtils.isEmpty(file.title)) {
hybridFileParcelable.setName(file.title);
}
```
替换为以下代码:
```
HybridFileParcelable hybridFileParcelable = new HybridFileParcelable(currentPath,
basePath, TextUtils.isEmpty(file.title) ? file.desc : file.title, file.permissions, file.date,
file.longSize, file.isDirectory, file.getMode(), "");
```
这样可以避免在判断 file.title 是否为空时多次调用 TextUtils.isEmpty() 方法。
其次,可以将 Map 的初始化改为以下方式:
```
Map<String, Object> map = new HashMap<>(2);
```
这样可以避免后续 put 操作导致 Map 扩容。
最后,建议对网络请求进行异常捕获处理,避免出现异常时程序崩溃。可以将以下代码:
```
api.saveFileBrowseRecord(body, 1)
.compose(HttpClient.observableIoToMain())
.as(HttpClient.bindLifecycle(this))
.subscribe(new HttpDefaultObserver<HttpResult<String>>() {
@Override
public void start() {
}
@Override
public void success(HttpResult<String> result) {
mCollects.put(file.desc, file.desc);
}
@Override
public void fail(HttpError e) {
showWhiteFailToast(e.getCode(), e.getMessage(), null);
}
@Override
public void finish() {
}
});
```
替换为以下代码:
```
try {
api.saveFileBrowseRecord(body, 1)
.compose(HttpClient.observableIoToMain())
.as(HttpClient.bindLifecycle(this))
.subscribe(new HttpDefaultObserver<HttpResult<String>>() {
@Override
public void start() {
}
@Override
public void success(HttpResult<String> result) {
mCollects.put(file.desc, file.desc);
}
@Override
public void fail(HttpError e) {
showWhiteFailToast(e.getCode(), e.getMessage(), null);
}
@Override
public void finish() {
}
});
} catch (Exception e) {
e.printStackTrace();
}
```
这样可以捕获网络请求中可能出现的异常,并在程序中进行处理。
阅读全文