Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getJSONObject throws JSONException under android 4.3 but works fine on android 4.4.2

Tags:

java

json

android

I'm currently developping an Android application displaying objects serialized in JSON. It works fine on my Nexus 5 (4.4.2) but crashes on a Samsung Galaxy S3 (4.3). The app crashes because a JSONException occurs specially on this device.

My JSON is generated from a Map, like this:

Map<String, Object> dataMap = new HashMap<String, Object>();
//
// filling the map
//
new JSONObject(map);

Logs :

Caused by: org.json.JSONException: Value {dealer={logo=null, name=Au café Bonheur}, title=Un mocha à 3€, price=3.0, feed_picture=https://shotgun-staging.s3.amazonaws.com/deals/tutorial1/feed, original_price=5.0} at deal of type java.lang.String cannot be converted to JSONObject
            at org.json.JSON.typeMismatch(JSON.java:100)
            at org.json.JSONObject.getJSONObject(JSONObject.java:577)
            at com.shotguntheapp.android.adapters.StackAdapter.getView(StackAdapter.java:33)
            at com.shotguntheapp.android.views.StackView.computeLayout(StackView.java:74)
            at com.shotguntheapp.android.views.StackView.setAdapter(StackView.java:60)
            at com.shotguntheapp.android.activities.FeedActivity.displayFeed(FeedActivity.java:91)
            at com.shotguntheapp.android.activities.Tuto1FeedActivity.reload(Tuto1FeedActivity.java:97)
            at com.shotguntheapp.android.activities.RequestsActivity.onResume(RequestsActivity.java:62)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209)
            at android.app.Activity.performResume(Activity.java:5450)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2909)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)

The JSON doesn't seems wrong. Line that raises Exception :

JSONObject deal = shotgun.getJSONObject(Api.JSON_DEAL);

I did not found other people having the same problem, so I have no idea of how this happens.

Thank you for your help.

like image 715
Greg Ballot Avatar asked Jan 25 '26 18:01

Greg Ballot


1 Answers

Indeed, the problem was on the syntax (that was right, but not working everywhere)

Creating an object from a Map generates a special JSON syntax that doesn't work on APIs below 19. That's why my application crashed under 4.3.

To have a right syntax from a Map, I used this subterfuge :

parseFeedData(new JSONObject(new Gson().toJson(object));
like image 126
Greg Ballot Avatar answered Jan 28 '26 06:01

Greg Ballot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!