ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.24k stars 955 forks source link

[Bug]: HttpRequestHandler.java for Android has parseJSON Exception problem #7400

Closed jaehyeong-gobox closed 4 days ago

jaehyeong-gobox commented 1 month ago

Capacitor Version

💊 Capacitor Doctor 💊

Latest Dependencies:

@capacitor/cli: 5.7.4 @capacitor/core: 5.7.4 @capacitor/android: 5.7.4 @capacitor/ios: 5.7.4

Installed Dependencies:

@capacitor/cli: 5.7.4 @capacitor/android: 5.7.4 @capacitor/core: 5.7.4 @capacitor/ios: 5.7.4

[success] iOS looking great! 👌 [success] Android looking great! 👌

Other API Details

No response

Platforms Affected

Current Behavior

when get JSON-return from api in android and it contains 10 or more digits value( >2,147,483,647), it will produce java.lang.NumberFormatException problem.

2024-04-15 11:04:44.414 20170-20442 Capacitor/Plugin        ionic.myapp                         E  For input string: "8000010570"
                                                                                                    java.lang.NumberFormatException: For input string: "8000010570"
                                                                                                        at java.lang.Integer.parseInt(Integer.java:797)
                                                                                                        at java.lang.Integer.parseInt(Integer.java:915)
                                                                                                        at com.getcapacitor.plugin.util.HttpRequestHandler.parseJSON(HttpRequestHandler.java:324)
                                                                                                        at com.getcapacitor.plugin.util.HttpRequestHandler.readData(HttpRequestHandler.java:252)
                                                                                                        at com.getcapacitor.plugin.util.HttpRequestHandler.buildResponse(HttpRequestHandler.java:222)
                                                                                                        at com.getcapacitor.plugin.util.HttpRequestHandler.request(HttpRequestHandler.java:431)
                                                                                                        at com.getcapacitor.plugin.CapacitorHttp$1.run(CapacitorHttp.java:66)
                                                                                                        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:487)
                                                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:264)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
                                                                                                        at java.lang.Thread.run(Thread.java:1012)

Expected Behavior

in https://github.com/ionic-team/capacitor/blob/main/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java

there's parseJSON


    public static Object parseJSON(String input) throws JSONException {
        JSONObject json = new JSONObject();
        try {
            if ("null".equals(input.trim())) {
                return JSONObject.NULL;
            } else if ("true".equals(input.trim())) {
                return true;
            } else if ("false".equals(input.trim())) {
                return false;
            } else if (input.trim().length() <= 0) {
                return "";
            } else if (input.trim().matches("^\".*\"$")) {
                // a string enclosed in " " is a json value, return the string without the quotes
                return input.trim().substring(1, input.trim().length() - 1);
            } else if (input.trim().matches("^-?\\d+$")) { // * * * * * * * * * * * * HERE * * * * * * * * * * *
                return Integer.parseInt(input.trim());
            } else if (input.trim().matches("^-?\\d+(\\.\\d+)?$")) {
                return Double.parseDouble(input.trim());
            } else {
                try {
                    return new JSObject(input);
                } catch (JSONException e) {
                    return new JSArray(input);
                }
            }
        } catch (JSONException e) {
            return input;
        }
    }

maybe we can use BigInt or change the if regex part?

Project Reproduction

https://github.com/ionic-team/capacitor/blob/main/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java

Additional Information

No response

ionitron-bot[bot] commented 1 month ago

This issue needs more information before it can be addressed. In particular, the reporter needs to provide a minimal sample app that demonstrates the issue. If no sample app is provided within 15 days, the issue will be closed. Please see the Contributing Guide for how to create a Sample App. Thanks! Ionitron 💙

meriturva commented 2 weeks ago

Same here with bigInt on json file using v8.x!

Ionitron commented 4 days ago

It looks like this issue didn't get the information it needed, so I'll close it for now. If I made a mistake, sorry! I am just a bot.

Have a great day! Ionitron 💙