目前我有一个用Java编写的文件上传器模块。我正在尝试使用promise,但在这种情况下,我只能在操作完成时发送一次数据。我的代码目前:
if (FileType.MESH_OBJ.equalsName(filename)) {
uploadHandler = new UploadFileTask() {
@Override
protected void onPostExecute(JSONObject jsonObject) {
objReady = true;
Log.d(appName, "Upload done: object");
if (objReady == true && imgReady == true && mtlReady == true && jpegReady == true) {
promise.resolve("finish");
}
}
};
} else if (FileType.PREVIEW_IMAGE.equalsName(filename)) {
uploadHandler = new UploadFileTask() {
@Override
protected void onPostExecute(JSONObject jsonObject) {
imgReady = true;
Log.d(appName, "Upload done: image");
if (objReady == true && imgReady == true && mtlReady == true && jpegReady == true) {
promise.resolve("finish");
}
}
};
}
在每种情况下我都会看到我必须检查一切准备就绪。不知何故,以下情况会很好:
每次上传的内容都会向React Native发送响应。在JS方面,我使用.then()
处理它,如下所示:
FileHandler.upload(...).then(...)
是否有任何解决方案可以解决此问题或仅存在请求 - 响应解决方案?
答案 0 :(得分:0)
您可以查看official doc以创建本机Java模块。
基本上,您将在Java端定义模块,如下所示:
public class FileHandler extends ReactContextBaseJavaModule {
@Override
public String getName() {
return "FileHandler";
}
@ReactMethod
public void upload(String fileName, Promise promise) {
if (FileType.MESH_OBJ.equalsName(filename)) {
uploadHandler = new UploadFileTask() {
@Override
protected void onPostExecute(JSONObject jsonObject) {
objReady = true;
Log.d(appName, "Upload done: object");
if (objReady == true && imgReady == true && mtlReady == true && jpegReady == true) {
promise.resolve("finish");
}
}
};
} else if (FileType.PREVIEW_IMAGE.equalsName(filename)) {
uploadHandler = new UploadFileTask() {
@Override
protected void onPostExecute(JSONObject jsonObject) {
imgReady = true;
Log.d(appName, "Upload done: image");
if (objReady == true && imgReady == true && mtlReady == true && jpegReady == true) {
promise.resolve("finish");
}
}
};
}
}
}
并在JavaScript方面调用它:
import { NativeModules } from 'react-native';
NativeModules.FileHandler.upload(...).then(...)