Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getFoods and getRecipes failing when only 1 result returned #13

Open
WMcCabe8 opened this issue Nov 19, 2019 · 1 comment
Open

getFoods and getRecipes failing when only 1 result returned #13

WMcCabe8 opened this issue Nov 19, 2019 · 1 comment

Comments

@WMcCabe8
Copy link

WMcCabe8 commented Nov 19, 2019

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!

WMcCabe8 added a commit to WMcCabe8/fatsecret4j that referenced this issue Apr 17, 2020
      a) getFoods() and getRecipes() failing when only 1 result returned
(fatsecret#13).
         - getFoods() is now searchFoods()
         - getRecipes() is now searchRecipes()
      b) getRecipe() fails when returning a Recipe without images
("recipe_images").
      c) issue with json request.
      d)issue with json request to now catch null returned objects, so
it won't break Android app.
@marioly7
Copy link

Have you fixed it? Please help! You can contact me [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants