【发布时间】:2026-01-29 20:35:01
【问题描述】:
当我扫描 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);
}
}
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
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
到处搜索,但我没有得到任何关于这个问题的结果,所以请帮助我:)
谢谢&问候 桑迪普·帕特尔
【问题讨论】:
-
你能告诉我 logcat 错误吗
-
JSONObject obj = new JSONObject(result.getContents()); //
-
见 logcat @PankajMundra
-
首先在线验证您的 json 响应(例如:jsoneditoronline.org)。您的回复格式不正确。
-
您的回复不是有效的 JSON,请参阅*.com/questions/10267910/…