天天看點

Cordova開發安卓app進行Bundle解析

(建立于2018/2/9)

最近在一個混合開發的項目中遇到的一種資料解析方式,使用Cordova進行原生和js的通信,原生接收js消息的方式

final BroadcastReceiver popReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                
            }
}
LocalBroadcastManager.getInstance(this).registerReceiver(popReceiver, new IntentFilter(Constants.LETTER_LIST));
           

是以收到js消息之後,都是從intent中拿,一般都是這樣

Bundle extras = intent.getExtras();
           

然後從bundle中擷取資料,如果是字元串,直接解析即可

String view = extras.getString(Constants.VIEW);
           

較複雜的資料,比如一個對象,一個集合j,就需要用到今天要說的方式來解析了 如下:

Bundle[{content=Bundle[{0=Bundle[{messages=Bundle[{0=Bundle[{type=us, content=啦咯啦咯啦咯, created_at=2018-02-09 16:55:06, headimgurl=https://naneng-user.oss-cn-shanghai.aliyuncs.com
/default_user_head.jpg}], 1=Bundle[{type=us, content=啦咯啦咯啦咯吐了老K, created_at=2018-02-09 
16:55:11, headimgurl=https://naneng-user.oss-cn-shanghai.aliyuncs.com/default_user_head.jpg}], 
2=Bundle[{type=us, content=T恤咯OK, created_at=2018-02-09 16:55:16, headimgurl=https://naneng-
user.oss-cn-shanghai.aliyuncs.com/default_user_head.jpg}]}], id=116, nickname=17621421360}]}]}]
           

解析方法:

//可以看到最外層是一個bundle,是以擷取到這個bundle,這裡的bundle即可以是對象也可以是集合,看括号的形式[]表示對象,{}表示集合,是以最外層bundle是對象
Bundle extras = intent.getExtras();
                    if (extras != null){
                        try {
                            JSONObject jsonObject = new JSONObject();
                            //以content為key的這個bundle是一個集合,可以看到,裡邊有三個元素,0,1,2分别為key
                            Bundle content = extras.getBundle("content");

                            for (int i=0;i<content.size();i++){
                                //0,1,2分别為key
                                Bundle bundle = content.getBundle(i+"");
                                Bundle messages = bundle.getBundle("messages");

                                JSONArray messageArray = new JSONArray();
                                for (int j=0;j<messages.size();j++){
                                    Bundle data = messages.getBundle(j + "");
                                    String type = data.getString("type");
                                    String dataContent = data.getString("content");
                                    String create_at = data.getString("created_at");
                                    String headimgurl = data.getString("headimgurl");

                                    JSONObject messageObj = new JSONObject();
                                    messageObj.put("type",type);
                                    messageObj.put("content",dataContent);
                                    messageObj.put("created_at",create_at);
                                    messageObj.put("headimgurl",headimgurl);

                                    messageArray.put(j,messageObj);
                                }
                                int id = bundle.getInt("id");
                                String nickname = bundle.getString("nickname");

                                JSONObject contentObj = new JSONObject();
                                contentObj.put("messages",messageArray);
                                contentObj.put("id",id);
                                contentObj.put("nickname",nickname);


                                jsonObject.put("content",contentObj);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
           

繼續閱讀