Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

react-native 安卓原生模块如何返回json或object类型?

    @ReactMethod
    public void fn(final Promise promise) {
          XXX obj = new XXX();
          // 请问这里如何返回 {a: obj.a, b: obj.b}
          promise.resolve({a: obj.a, b: obj.b});
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1.如果传递对象:

 @ReactMethod
 public void getUserInfo(Callback callback){
     WritableMap map = Arguments.createMap();
     map.putString("islogin",MainApplication.getApplication().isLogin()+"");
     map.putString("nick_name", getApplication().getNickname());
     map.putString("user_name", getApplication().getUsername());
     map.putString("avatar_url", getApplication().getTouxiang());
     map.putString("access_token",getApplication().getAccess_token());
     map.putString("uid",getApplication().getUid()+"");
     map.putString("gender_str", getApplication().getSex());
     callback.invoke(map);
 } 

2.如果传递json数组:

 @ReactMethod
 public void getAllGameList(Callback callback){
     ArrayList<Game> games = MainApplication.getApplication().getGames();
     WritableArray arrays = Arguments.createArray();
     for (Game game:games){
         WritableMap map = Arguments.createMap();
         map.putString("name",game.name);
         map.putString("thumbnail_url",game.thumbnail_url);
         ....
         arrays.pushMap(map);
     }
     callback.invoke(arrays);
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...