Android Question Issue with JSON parser error handling

Dr. Guaranexx

New Member
Licensed User
Longtime User
Just for a little bit of background information regarding my situation, I am using the b4aZXing library to scan a QR code on ID cards. The data in the QR code contains a short JSON string which is parsed to extract some basic information stored on the card. The JSON library does a great job of extracting the data from the JSON string with no issues.

However, my problem has to do with handling situations where an invalid QR code is scanned. For example, passing a non-JSON string, such as a URL, will cause the application to crash.

Is there a way that I can either test for this condition or capture the error? At the moment I am using a simple check to see if the first and last characters are '{' and '}', respectively. However, this is not an ideal solution since the JSON string could still be invalid.

Any help is greatly appreciated.

Here's a snippet of the code that i'm using:
B4X:
Sub LoginScan_result(ScanType As String, ScanValues As String)
    ' If the scan type is not QR_CODE, then ask to scan again
    If ScanType.ToLowerCase <> "qr_code" Then
        LoginScanner.BeginScan("LoginScan")
        ToastMessageShow("Scan failed. Please try again.",True)
        Return False
    End If
 
    Dim jsonString As String = ScanValues.Trim
 
    If jsonString.CharAt(0) <> "{" OR jsonString.CharAt(jsonString.Length-1) <> "}" Then
        LoginScanner.BeginScan("LoginScan")
        ToastMessageShow("Scan failed. Please try again.",True)
        Return False
    End If
 
    Dim json As JSONParser
    Dim m As Map
 
    json.Initialize(jsonString)
    m = json.NextObject ' This line throws an error if the JSON data is not valid
End Sub

Also, below is a copy of the error message:
B4X:
m = json.NextObject
java.lang.RuntimeException: JSON Object expected.
    at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:47)
    at b4a.example.main._loginscan_result(main.java:608)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at ice.zxing.b4aZXingLib$1.ResultArrived(b4aZXingLib.java:51)
    at anywheresoftware.b4a.BA$4.run(BA.java:465)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:377)
    at b4a.example.main$ResumeMessage.run(main.java:219)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
 
Last edited:
Top