7

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:abc@example.com
EMAIL;WORK;INTERNET:abc@example.com
URL:http://ABC@ABC.COM
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

Nick
  • 1,127
  • 2
  • 15
  • 40
Sandeep Patel
  • 202
  • 3
  • 16

4 Answers4

2

Here is my solution for you. If you getting response data like

        "
        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
        " this than forgot the json object concept.

Use the vcard library. you can get vcard library from here https://github.com/mangstadt/ez-vcard

By using this library you can get any contact data you want using below code and you can set in your editext.

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();

Still if you have any doubt how to do this. You can ask me further.

Ali
  • 3,346
  • 4
  • 21
  • 56
Maulik Patel
  • 717
  • 1
  • 10
  • 22
  • I want get QR code data not static data in your example u print static data so it is possible to get dynamic data – Sandeep Patel Mar 20 '18 at 14:07
  • It will work with dynamic data. If QR code give multiple contact data stilll it will work. if your qr code generate vcf formated data than it will work for sure. When you scan the qr code what information it gives? – Maulik Patel Mar 21 '18 at 05:37
  • This information its give me on catch block toast **`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`** – Sandeep Patel Mar 21 '18 at 07:22
  • You are getting this info in catch while executing this line. JSONObject obj = new JSONObject(result.getContents()); but I think you can get the data by result.geContent(). No need to parse the json now. After getting data in result.getContent(). You can use this line VCard vcard = Ezvcard.parse(str).first(); String fullName = vcard.getFormattedName().getValue(); String lastName = vcard.getStructuredName().getFamily(); You can follow my solution – Maulik Patel Mar 21 '18 at 07:33
  • yes in this line i'll get data but it's not pass on to the `obj` **Object** – Sandeep Patel Mar 21 '18 at 07:37
  • No need to pass data into obj. Now use this library https://github.com/mangstadt/ez-vcard VCard vcard = Ezvcard.parse(str).first(); String fullName = vcard.getFormattedName().getValue(); String lastName = vcard.getStructuredName().getFamily(); – Maulik Patel Mar 21 '18 at 07:38
2

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());
                ...
PhilLab
  • 4,777
  • 1
  • 25
  • 77
0

Check your string data. Make sure Your String data is json object.

Print in log your string and check whether it is json or not.

Pankaj Mundra
  • 1,401
  • 9
  • 25
0

The content is not JSON, so you cannot convert it to JSONObject. You may need a parser for Vcard. Search for some library in Github or write your own.

Fung
  • 961
  • 1
  • 8
  • 17