capacitor-community / http

Community plugin for native HTTP
MIT License
208 stars 136 forks source link

new JSONArray() trow error while parsing array of non primitive types #19

Open Mporuben opened 4 years ago

Mporuben commented 4 years ago

Describe the bug Out BE is sending array of objects in response. While plugin is processing response it trow this error in Android Studio console 2020-06-23 11:31:15.059 14364-14445/com.my.app E/Capacitor/Plugin: Error org.json.JSONException: Not a primitive array: class java.lang.String at org.json.JSONArray.<init>(JSONArray.java:118) at com.getcapacitor.JSArray.<init>(JSArray.java:21) at com.getcapacitor.plugin.http.Http.buildResponse(Http.java:395) at com.getcapacitor.plugin.http.Http.get(Http.java:83) at com.getcapacitor.plugin.http.Http.request(Http.java:64) at java.lang.reflect.Method.invoke(Native Method) at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99) at com.getcapacitor.Bridge$1.run(Bridge.java:520) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.os.HandlerThread.run(HandlerThread.java:67)

To Reproduce

  1. Try to get array of objects from server

Smartphone:

Additional context Example response from BE [ { "id": "XXXXX", "name": "xxxx", "info": "xyz", "available-from": "2020-05-01" }, { "id": "YYYY", "name": "yyyyy", "info": "yzx", "available-from": "2020-05-01" } ]

Error appears to be in Http.java. While plugins is trying to buildResponse that should be send back to "webview". JSArray jsonValue = new JSArray(builder.toString());

We made a quick fix just as POC. Basically we just wrapped array in object and parse it like that.

//JSArray jsonValue = new JSArray(builder.toString());
JSObject jsonValue = new JSObject("{\"_CAPACITOR_FIX_ARRAY\": " + builder+ "}");
ret.put("data", jsonValue);
thomasvidas commented 3 years ago

Hello 👋

Is this only an issue on Android, or is it affecting iOS and Web as well?

Mporuben commented 3 years ago

Hi, it's happening only on Android