Package org.json.me

Examples of org.json.me.JSONObject


            throws JSONException, IOException {
        log("parseMessage(NDEFMessage)");
        String record_type = "?";
        int typeNameFormat = NDEFRecord.TNF_WELL_KNOWN;

        JSONObject result = new JSONObject();
        /*
         * The NDEF Message may consist of a number of NDEF records
         */

        NDEFRecord[] records = _message.getRecords();
        /*
         * This is the number of NDEF records in the NDEF message
         */
        int numRecords = records.length;

        log("Parsing #records=" + numRecords);

        /*
         * Only unpick the message if it contains a non-zero number of records
         */
        JSONArray recordsArray = new JSONArray();
        result.put(Constants.NDEF_RECORDS, recordsArray);

        if (numRecords > 0) {
            /*
             * Work our way through each record in the message in turn
             */
            for (int j = 0; j < numRecords; ++j) {
                log("parseMessage record #" + j);

                NDEFRecord currentRecord = records[j];
                JSONObject jsonRecord = new JSONObject();
                recordsArray.put(jsonRecord);

                log("Processing NDEF record#=" + j);

                typeNameFormat = currentRecord.getTypeNameFormat();
                record_type = records[j].getType();

                if (typeNameFormat == NDEFRecord.TNF_WELL_KNOWN) {
                    if (Constants.NDEF_SMART_POSTER_TYPE.equals(records[j]
                            .getType())) {
                        mergeInto(jsonRecord, parseSmartPoster(records[j]));
                    } else if (Constants.NDEF_TEXT_TYPE.equals(records[j]
                            .getType())) {
                        mergeInto(jsonRecord, parseText(records[j]));
                    } else if (Constants.NDEF_URI_TYPE.equals(records[j]
                            .getType())) {
                        mergeInto(jsonRecord, parseURI(records[j]));
                    } else {
                        // Gc, Hr, Hs, Hc, Sig are not currently parsed by this
                        // implementation...
                    }
                } else if (typeNameFormat == NDEFRecord.TNF_MEDIA) {
                    mergeInto(jsonRecord, parseMediaRecord(records[j]));
                } else if (typeNameFormat == NDEFRecord.TNF_EXTERNAL) {
                    NDEFMessage message = new NDEFMessage(
                            records[j].getPayload());
                    mergeInto(jsonRecord, parseMessage(message));
                }

                if (!jsonRecord.has(Constants.NDEF_PAYLOAD64)) {
                    mergeInto(jsonRecord, parseGenericRecord(records[j]));
                }

                // Put the first record at the top level - to simplify
                // accessing.
View Full Code Here


        synchronized (_messages) {
            message = (NDEFMessage) _messages.elementAt(_messages.size() - 1);
            _messages.removeElementAt(_messages.size() - 1);
        }

        JSONObject toUse;
        try {
            toUse = parseMessage(message);
            try {
                _js_bridge.useNDEFMessage(
                        toUse.getInt(Constants.NDEF_TYPE_NAME_FORMAT),
                        toUse.getString(Constants.NDEF_TYPE), toUse.toString());
            } catch (JSONException j) {
                log("Could not send object back to event handler", j);
            }
        } catch (Exception e) {
            String record_type = "?";
View Full Code Here

    }

    protected JSONObject parseURI(NDEFRecord record) throws JSONException,
            IOException {
        log("parseURL(NDEFRecord)");
        JSONObject toReturn = parseGenericRecord(record);
        byte[] payload = record.getPayload();
        StringBuffer uriBuffer = new StringBuffer();
        uriBuffer.append(URLPrefixes.getPrefix(payload[0] & 0xff));
        uriBuffer.append(new String(payload, 1, payload.length - 1, "UTF-8"));
        toReturn.put(Constants.NDEF_URI, uriBuffer.toString());
        return toReturn;
    }
View Full Code Here

    }

    protected JSONObject parseGenericRecord(NDEFRecord record)
            throws JSONException, IOException {
        log("parseGenericRecord(NDEFRecord)");
        JSONObject toReturn = new JSONObject();
        toReturn.putOpt(Constants.NDEF_ID, record.getId());
        toReturn.putOpt(Constants.NDEF_TYPE, record.getType());
        toReturn.put(Constants.NDEF_TYPE_NAME_FORMAT,
                record.getTypeNameFormat());

        byte[] payloadBytes = record.getPayload();
        JSONArray bytePayload = new JSONArray();

        for (int i = 0; i < payloadBytes.length; i++) {
            byte b = payloadBytes[i];
            bytePayload.put(b & 0xff);
        }

        toReturn.put("payload", bytePayload);
        return toReturn;
    }
View Full Code Here

        if (textLength > 0) {
            text = new String(payload, 1 + languageCodeLength, textLength,
                    isUTF16 ? "UTF-16" : "UTF-8");
        }

        JSONObject toReturn = parseGenericRecord(record);
        toReturn.putOpt(Constants.NDEF_TEXT_LANGUAGE_CODE, languageCode);
        toReturn.putOpt(Constants.NDEF_TEXT_VALUE, text);
        return toReturn;
    }
View Full Code Here

    }

    protected JSONObject parseSmartPoster(NDEFRecord record)
            throws JSONException, IOException {
        log("parseSmartPoster(NDEFRecord)");
        JSONObject toReturn = new JSONObject();
        JSONArray recordsArray = new JSONArray();
        toReturn.put("records", recordsArray);
        // testing

        // if (true)
        // throw new JSONException("Fictitious JSON problem");

        // if(true)
        // throw new
        // UnsupportedEncodingException("Fictitious encoding problem");

        // if(true)
        // throw new BadFormatException("Fictitious format problem");

        // if(true)
        // throw new NFCException("Fictitious NFC problem");

        log("Recognised a Smart Poster Message");
        // try {
        NDEFMessage smartPosterMessage = new NDEFMessage(record.getPayload());
        NDEFRecord[] spRecords = smartPosterMessage.getRecords();
        int numSpRecords = spRecords.length;
        log("Parsing smartposter #records=" + numSpRecords);

        if (numSpRecords > 0) {
            log("Parsing Smart Poster Message");
            for (int k = 0; k < numSpRecords; ++k) {
                log("parseSmartPoster record #" + k);
                JSONObject currentRecordJSON = parseGenericRecord(spRecords[k]);
                recordsArray.put(currentRecordJSON);
                log("Parsing SP record#=" + k);
                byte[] spPayloadBytes = spRecords[k].getPayload();

                int tnf = spRecords[k].getTypeNameFormat();
                String type = spRecords[k].getType();

                log("entering parseSmartPoster if");

                if (tnf == NDEFRecord.TNF_WELL_KNOWN) {
                    if (Constants.NDEF_TEXT_TYPE.equals(type)) {
                        log(Constants.NDEF_TEXT_TYPE);
                        JSONObject text = parseText(spRecords[k]);
                        mergeInto(currentRecordJSON, text);
                        if (text != null) {
                            toReturn.putOpt(Constants.NDEF_TEXT,
                                    text.get(Constants.NDEF_TEXT_VALUE));
                            getOrCreateArray(toReturn,
                                    Constants.NDEF_TEXT_VALUES).put(text);
                        }
                    } else if (Constants.NDEF_URI_TYPE.equals(type)) {
                        log(Constants.NDEF_URI_TYPE);
                        JSONObject uri = parseURI(spRecords[k]);
                        mergeInto(currentRecordJSON, uri);
                        toReturn.put(Constants.NDEF_URI,
                                uri.get(Constants.NDEF_URI));
                        toReturn.put(Constants.NDEF_URI_LEGACY,
                                uri.get(Constants.NDEF_URI));
                    } else if (Constants.NDEF_SMART_POSTER_RECOMMENDED_ACTION_TYPE
                            .equals(type)) {
                        log("Reading an action");
                        if (spPayloadBytes.length != 1) {
                            log("Incorrect length noted for 'action' record: "
View Full Code Here

    }

    protected JSONObject parseMediaRecord(NDEFRecord ndefRecord)
            throws JSONException, IOException {
        log("parseMediaRecord(NDEFRecord)");
        JSONObject toReturn = parseGenericRecord(ndefRecord);
        byte[] payLoad = ndefRecord.getPayload();
        String base64 = Base64OutputStream.encodeAsString(payLoad, 0,
                payLoad.length, false, false);
        toReturn.put(Constants.NDEF_MEDIA_MIME_TYPE, ndefRecord.getType());
        toReturn.put(Constants.NDEF_MEDIA_MIME_BODY, base64);
        toReturn.put(Constants.NDEF_MEDIA_DATA_URI,
                "data:" + ndefRecord.getType() + ";base64," + base64);
        return toReturn;
    }
View Full Code Here

        if(args.length > 0) {
            if (args[0] instanceof String) {
                if (((String)args[0]).indexOf("{")==-1) {
                    record_type = (String) args[0];
                } else {
                    JSONObject params = new JSONObject((String)args[0]);
                    if (params.has(Constants.NDEF_TYPE)) {
                        record_type = params.getString(Constants.NDEF_TYPE);
                    } else {
                        log("Defaulting to Sp (no type in JSON Object)");
                    }
                    if (params.has(Constants.NDEF_TYPE_NAME_FORMAT)) {
                        typeNameFormat = params.getInt(Constants.NDEF_TYPE_NAME_FORMAT);
                    }
                }
            } else {
                log("args[0] is an invalid type of Object");
                return Boolean.FALSE;
View Full Code Here

            if (args[2] instanceof String) {
                if (((String) args[2]).indexOf("{") == -1) {
                    record_type = (String) args[2];
                } else {
                    try {
                        JSONObject params = new JSONObject((String) args[2]);
                        if (params.has(Constants.NDEF_TYPE)) {
                            record_type = params.getString(Constants.NDEF_TYPE);
                        } else {
                            log("Defaulting to Sp (no type in JSONObject)");
                        }
                        if (params.has(Constants.NDEF_TYPE_NAME_FORMAT)) {
                            typeNameFormat = params
                                    .getInt(Constants.NDEF_TYPE_NAME_FORMAT);
                        }
                    } catch (JSONException je) {
                        log("Could not decode param", je);
                        return Boolean.FALSE;
View Full Code Here

   
    private void makeCallback(){
   
      String result = "";
     
      JSONObject j = new JSONObject();
      try {
      j.put("latitude", Double.toString(currSelection.getLat()));
      j.put("longitude", Double.toString(currSelection.getLon()));
      result = j.toString();
    } catch (JSONException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }catch (Exception e){
     
View Full Code Here

TOP

Related Classes of org.json.me.JSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.