Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONObject don't Get data from QR code

Tags:

java

json

android

When I scan QR code it's getting a data on result.getContents but didn't pass the data on JSONObject obj just directing go on Catch block

data get pass to this line JSONObject obj = new JSONObject(result.getContents()); but when start to Debug then its not pass the data on 'obj' in above line.

here is my code :

@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);
        }
    }

Logcat see below :

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

JSON Response Data like below code:

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:[email protected]
EMAIL;WORK;INTERNET:[email protected]
URL:http://[email protected]
ADR:;;sample address;ss;;102103;US
ORG:ss
END:VCARD

Search everywhere but I don't get any result for this problem so plzzz help me:)

Thanks&Regards Sandeep Patel

like image 411
Sandeep Patel Avatar asked Mar 09 '18 05:03

Sandeep Patel


People also ask

How do I get data from a QR code?

Scanning a QR code using your device is straightforward: Open the QR reader application or the camera on your smartphone. Point it at the QR code – you should be able to point your camera from any angle and still receive the necessary information.

Why are my QR codes not working?

Here are some reasons why a QR code is not working on Android devices: Your Android device isn't running Android 9 or higher. If your device can't run Android 9 or higher, you can download a third-party app to scan QR codes. You can also check and update your Android version.

Can QR store JSON?

As you say, if you want to store raw JSON in the QR code, you have to stringify it first using JSON. stringify(json) . When reading the QR code, you have no choice but to parse the string to get the raw JSON back, using JSON.

How the data stored in QR code is read?

In QR codes, information is encoded in the arrangement of squares. Either way, data transforms into a machine-readable arrangement of visual elements. And upon scanning by an optical scanning device, the data translates back to its original form.


1 Answers

Your data is not JSON, see what JSONLint reports on your output. The errororg.json.JSONException informs you, that in the beginning of your string, the phrase "BEGIN" is not allowed in Json.

{
    "json": "looks like this",
    "key": "value"
}

As your data seems to be vCard, you need a different parsing library, e.g. 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();

So in your code snippet:

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());
                ...
like image 177
PhilLab Avatar answered Oct 18 '22 20:10

PhilLab