}
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: "