在React-native桥中将字节数组从java传递到javascript

时间:2017-02-01 07:32:54

标签: react-native

我正在尝试通过Promise将一个bytearray对象传递给反应中的Javascript - native。

代码:

  RawRequest request = new RawRequest(Request.Method.GET, path, new 


 ResponseListener<byte[]>() {

        @Override
        public void onResponse(byte[] bytes) {

           //Let's pass the response in the form of bytes
             promise.resolve(bytes);

    Log.d(TAG,"The response in bytes length is " + bytes.length);
        }

    }, new ErrorListener() {
        @Override
        public void onError(HttpError httpError) {

        }
    })

例外:

致命的例外:主要                                                                      进程:com.listviewdbproject,PID:5965                                                                      java.lang.RuntimeException:无法转换类型为[B]的参数                                                                          at com.facebook.react.bridge.Arguments.fromJavaArgs(Arguments.java:57)                                                                          在com.facebook.react.bridge.CallbackImpl.invoke(CallbackImpl.java:29)                                                                          在com.facebook.react.bridge.PromiseImpl.resolve(PromiseImpl.java:32)                                                                          在com.example.creosdk.HttpModule $ 1.onResponse(HttpModule.java:80)                                                                          在com.example.creosdk.HttpModule $ 1.onResponse(HttpModule.java:76)                                                                          在com.creo.fuel.uttp.http.toolbox.RawRequest.deliverResponse(RawRequest.java:46)                                                                          在com.creo.fuel.uttp.http.toolbox.RawRequest.deliverResponse(RawRequest.java:16)                                                                          在com.creo.fuel.uttp.http.delivery.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:100)                                                                          在android.os.Handler.handleCallback(Handler.java:815)                                                                          在android.os.Handler.dispatchMessage(Handler.java:104)                                                                          在android.os.Looper.loop(Looper.java:194)                                                                          在android.app.ActivityThread.main(ActivityThread.java:5616)                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                          在java.lang.reflect.Method.invoke(Method.java:372)                                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:959)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

1 个答案:

答案 0 :(得分:0)

您无法直接解析Array和ArrayList数据结构。 React Native专门提供WritableNativeArray来完成此任务。据我所知,它还没有在任何地方记录,但你可以找到它here。 以下代码适用于我,您可以扩展它以满足您的要求:

@ReactMethod
public void sendArrays(Promise promise) {

    WritableArray arr = new WritableNativeArray();

    arr.pushString("hello");
    arr.pushString("world");

    promise.resolve(arr);
}

在JS方面:

import { NativeModules } from 'react-native';

const { someNativeModule } = NativeModules;

const getArrays = async () => {
  try {
    const response = await someNativeModule.sendArrays();

    return response;
    } catch (err) {
    // handle errors
  }
};