JSONObject不从QR代码获取数据

San*_*tel 6 java android json

当我扫描QR码时,它正在获取数据,result.getContents但是没有传递JSONObject上的数据,obj只是指示进行Catch阻止

数据传递到此行,JSONObject obj = new JSONObject(result.getContents());但是当开始时Debug它不会传递上面一行中'obj'的数据.

这是我的代码:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            //if qrcode has nothing in it
            if (result.getContents() == null) {
                Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
            } else {
                //if qr contains data
                try {
                    //converting the data to json
                    JSONObject obj = new JSONObject(result.getContents());
                    //setting values to textviews

                    if(obj.has("FN")) {
                        etFirstName.setText(obj.getString("FN"));
                    }
                    if(obj.has("N")) {
                        etLastName.setText(obj.getString("LN"));
                    }
                    if(obj.has("TITLE")) {
                        etTitle.setText(obj.getString("TITLE"));
                    }
                    if(obj.has("STATUS")) {
                        etStatus.setText(obj.getString("STATUS"));
                    }
                    if(obj.has("EMAIL")) {
                        etEmail.setText(obj.getString("EMAIL"));
                    }
                    if(obj.has("TEL;TYPE=work")) {
                        etPhoneHome.setText(obj.getString("TEL;TYPE=work"));
                    }
                    if(obj.has("TEL;TYPE=cell")) {
                        etPhonePrimary.setText(obj.getString("TEL;TYPE=cell"));
                    }
                    if(obj.has("ADR;TYPE=work")) {
                        etAddressLine1.setText(obj.getString("ADR;TYPE=work"));
                    }
                    if(obj.has("Street")) {
                        etAddressLine2.setText(obj.getString("Street"));
                    }
                    if(obj.has("Street")) {
                        etCity.setText(obj.getString("Street"));
                    }
                    if(obj.has("zip")) {
                        etZip.setText(obj.getString("zip"));
                    }


                } catch (JSONException e) {

                    Log.e(TAG, "notification= error" + e.toString());
                    e.printStackTrace();

                    /*etFirstName.setText(result.getContents());
                    etLastName.setText(result.getContents());
                    etTitle.setText(result.getContents());
                    etEmail.setText(result.getContents());*/
                    //if control comes here
                    //that means the encoded format not matches
                    //in this case you can display whatever data is available on the qrcode
                    //to a toast
                    Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Logcat见下文:

03-09 11:23:06.909 297-678/? E/SimpleSoftOMXComponent: 3
03-09 11:23:07.127 297-665/? E/audio_a2dp_hw: adev_set_parameters: ERROR: set param called even when stream out is null
03-09 11:23:12.899 26980-26980/com.example.crm E/AddContactActivity: notification= errororg.json.JSONException: Value BEGIN of type java.lang.String cannot be converted to JSONObject
03-09 11:23:13.040 297-8371/? E/SimpleSoftOMXComponent: 1
03-09 11:23:13.040 297-8371/? E/SimpleSoftOMXComponent: 2
03-09 11:23:13.041 297-8371/? E/SimpleSoftOMXComponent: 3
Run Code Online (Sandbox Code Playgroud)

JSON响应数据如下代码:

BEGIN:VCARD,
VERSION:2.1
FN:sss sss
N:sss;sss
TITLE:PHD
TEL;CELL:1111111111
TEL;WORK;VOICE:2222222222
TEL;HOME;VOICE:8888888888
EMAIL;HOME;INTERNET:abc@example.com
EMAIL;WORK;INTERNET:abc@example.com
URL:http://ABC@ABC.COM
ADR:;;sample address;ss;;102103;US
ORG:ss
END:VCARD
Run Code Online (Sandbox Code Playgroud)

搜索到处都是,但我没有得到任何结果这个问题所以plzzz帮助我:)

谢谢和问候Sandeep Patel

Phi*_*Lab 2

您的数据不是 JSON,请查看JSONLint在您的输出中报告的内容。通知errororg.json.JSONException您,在字符串的开头,Json 中不允许使用短语“BEGIN”。

{
    "json": "looks like this",
    "key": "value"
}
Run Code Online (Sandbox Code Playgroud)

由于您的数据似乎是 vCard,因此您需要不同的解析库,例如https://github.com/mangstadt/ez-vcard

String str =
"BEGIN:VCARD\r\n" +
"VERSION:4.0\r\n" +
"N:Doe;Jonathan;;Mr;\r\n" +
"FN:John Doe\r\n" +
"END:VCARD\r\n";

VCard vcard = Ezvcard.parse(str).first();
String fullName = vcard.getFormattedName().getValue();
String lastName = vcard.getStructuredName().getFamily();
Run Code Online (Sandbox Code Playgroud)

所以在你的代码片段中:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        //if qrcode has nothing in it
        if (result.getContents() == null) {
            Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
        } else {
            //if qr contains data
            try {
                //converting the data to vCard object
                VCard vcard = Ezvcard.parse(result.getContents()).first();
                //setting values to textviews

                // "given name" is the first name
                etFirstName.setText(vcard.getStructuredName().getGiven());

                // "family name" is the last name
                etLastName.setText(vcard.getStructuredName().getFamily());
                ...
Run Code Online (Sandbox Code Playgroud)