fatsecret/fatsecret4j

getFoods and getRecipes failing when only 1 result returned

WMcCabe8 opened this issue · 1 comments

For example, in my app I'm using :
implementation 'com.fatsecret4j:fatsecret-platform:2.0' implementation 'com.android.volley:volley:1.1.0'

I am calling req.getFoods(requestQueue, search, 0); within one food search activity and display in ListView; however, whenever the results from searching is 1 the activity fails with the following :

THIS IS FROM SEARCHING USING ---> "fbfx"

com..... E/AndroidRuntime: FATAL EXCEPTION: main Process: com......, PID: 22297 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)  Caused by: org.json.JSONException: Value {"brand_name":"Max Muscle","food_description":"Per 1 scoop - Calories: 72kcal | Fat: 0.00g | Carbs: 17.00g | Protein: 0.00g","food_id":"3577034","food_name":"FBX","food_type":"Brand","food_url":"https:\/\/www.fatsecret.com\/calories-nutrition\/max-muscle\/fbx"} at food of type org.json.JSONObject cannot be converted to JSONArray at org.json.JSON.typeMismatch(JSON.java:101) at org.json.JSONObject.getJSONArray(JSONObject.java:591) at com.fatsecret.platform.services.android.Request$1.onResponse(Request.java:182) at com.fatsecret.platform.services.android.Request$1.onResponse(Request.java:156)

I researched this more and it is coming from com/fatsecret/platform/services/android/Request.java line 182: JSONArray foodArray = foods.getJSONArray("food"); when the Request class is trying to convert a JSONObject into an array when the setup is different.

This ONLY happens when 1 result is returned, the JSON response is different than when returning more than 1 result.

For example, with 1 result using "fbfx" as a search query the result (JSON format) is:
{ "food": { "brand_name": "Max Muscle", "food_description": "Per 1 scoop - Calories: 72kcal | Fat: 0.00g | Carbs: 17.00g | Protein: 0.00g", "food_id": "3577034", "food_name": "FBX", "food_type": "Brand", "food_url": "https://www.fatsecret.com/calories-nutrition/max-muscle/fbx" }, "max_results": "50", "page_number": "0", "total_results": "1" }

Now, with 2 results using "fbf" as a search query (2 results returned) the JSON format is:
{ "food": [ { "brand_name": "Max Muscle", "food_description": "Per 1 scoop - Calories: 72kcal | Fat: 0.00g | Carbs: 17.00g | Protein: 0.00g", "food_id": "3577034", "food_name": "FBX", "food_type": "Brand", "food_url": "https://www.fatsecret.com/calories-nutrition/max-muscle/fbx" }, { "brand_name": "FGF Brands", "food_description": "Per 2.2 oz - Calories: 160kcal | Fat: 3.50g | Carbs: 28.00g | Protein: 4.00g", "food_id": "27445", "food_name": "Fabulous Flats Tandoori Naan", "food_type": "Brand", "food_url": "https://www.fatsecret.com/calories-nutrition/fgf-brands/fabulous-flats-tandoori-naan" } ], "max_results": "50", "page_number": "0", "total_results": "2" }

Notice that the difference from the 1 result to the 1> results is the "food" JSON object changes from an object (1 result) to an array with "[ ]" surrounding the multiple results...


For req.getRecipes(requestQueue, search, 0); I am getting the exact same error when only 1 result is returned. You can see this when searching "Sushi". The Request class fails again when trying to convert the JSONObject to a JSONArray.

I am no expert, but I believe both of these can be fixed with the com/fatsecret/platform/services/android/Request.java class updating how to bring in a JSONObject when only 1 result is returned both on line 182 and 214.

Please if anyone has a workaround in the meantime or if you need any more info about this issue, let me know!

Much appreciated!

Have you fixed it? Please help! You can contact me mariolyv7@gmail.com