Reputation: 202
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
Upvotes: 7
Views: 4376
Reputation: 969
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.
Upvotes: 0
Reputation: 5027
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());
...
Upvotes: 2
Reputation: 717
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:[email protected]
EMAIL;WORK;INTERNET:[email protected]
URL:http://[email protected]
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.
Upvotes: 2
Reputation: 1599
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.
Upvotes: 0