React Native无法返回数组

时间:2019-02-18 15:02:43

标签: javascript java reactjs react-native react-native-android

好的。

我正在使用BrotherSDK构建一个小型POC,我要做的就是返回标签打印机的列表及其纸张尺寸。 我读过为了做到这一点,我必须使用ReactBridge中的Promise.resolve函数。

我尝试过,但是在模拟器上却出现这样的错误。

simulator

我的Java代码如下:

 @ReactMethod
    public void getPrinterList(final Promise promise) {
        try {
            ArrayList<String> printerList = new ArrayList<>();
            Collections.addAll(printerList, "QL710W", "QL720NW",
                    "PTE550W",
                    "PTE500",
                    "PT750W",
                    "PTD800W",
                    "PTE800W",
                    "PTE850TKW",
                    "PTP900W",
                    "PTP960NW",
                    "QL810W",
                    "QL800",
                    "QL820NWB",
                    "PTP300BT",
                    "QL1100",
                    "QL1110NWB",
                    "QL1115NWB",
                    "PTP710BT");
            String [] retArray = new String[printerList.size()];
            retArray = printerList.toArray(retArray);
            WritableArray promiseArray = Arguments.createArray();
            for(int i=0; i < retArray.length; i++){
                promiseArray.pushString(retArray[i]);
            }
            promise.resolve(promiseArray);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @ReactMethod
    public void getPaperSizes(final Promise promise) {
        try {
            List<String> paperSizeList = new ArrayList<>();
            Collections.addAll(paperSizeList, "W17H54",
                    "W17H87",
                    "W23H23",
                    "W29H90",
                    "W29H42","W38H90",
                    "W39H48",
                    "W52H29",
                    "W54H29",
                    "W62H29",
                    "W62H100",
                    "W60H86",
                    "W12","W29", "W38",
                    "W50", "W54",
                    "W62", "W62RB");
            String [] retArray = new String[paperSizeList.size()];
            retArray = paperSizeList.toArray(retArray);
            WritableArray promiseArray = Arguments.createArray();
            for(int i = 0; i < retArray.length; i++) {
                promiseArray.pushString(retArray[i]);
            }
            promise.resolve(promiseArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

而我的JS代码如下所示:

// Two arrays
printerList = [];
paperSizes = [];

// Functions

  getPrinterList = async () => {
    this.printerList = await MyRNModule.getPrinterList();
  }

  getPaperSizes = async () =>  {
    this.paperSizes = await MyRNModule.getPaperSizes();
  }

1 个答案:

答案 0 :(得分:1)

错误消息说明了一切。

  

com.facebook.react.bridge。可读nativemap无法转换为java.lang.string

您返回的是NetworkGuard就好了,只是期望会返回array

要解决此问题,您可以通过join将数组转换为字符串。