From 60c3cae47d93a8df71876e08707b88d510626025 Mon Sep 17 00:00:00 2001 From: PaulStets Date: Sat, 14 Apr 2018 16:38:06 +0300 Subject: [PATCH] Contents json parsing fix --- .../WikivoyageArticleDialogFragment.java | 15 ++++++++++++++- .../wikivoyage/data/WikivoyageJsonParser.java | 19 ++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java index dc929bca03..8a68a226d5 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java @@ -49,7 +49,20 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen "\n" + "\n" + "\n" + ""; + "window.location.hash = id;}" + + "var coll = document.getElementsByTagName(\"H2\");" + + "var i;" + + "for (i = 0; i < coll.length; i++) {" + + " coll[i].addEventListener(\"click\", function() {" + + " this.classList.toggle(\"active\");" + + " var content = this.nextElementSibling;" + + " if (content.style.display === \"block\") {" + + " content.style.display = \"none\";" + + " } else {" + + " content.style.display = \"block\";" + + " }" + + " });" + + "}"; private static final String FOOTER_INNER = ""; private long cityId = NO_VALUE; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java index 533cb511dc..f5d012aa75 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java @@ -1,6 +1,7 @@ package net.osmand.plus.wikivoyage.data; import android.support.annotation.Nullable; +import android.util.Log; import org.json.JSONArray; import org.json.JSONException; @@ -11,9 +12,12 @@ import java.util.List; public class WikivoyageJsonParser { + private static final String TAG = WikivoyageJsonParser.class.getSimpleName(); + private static final String HEADERS = "headers"; private static final String SUBHEADERS = "subheaders"; private static final String LINK = "link"; + public static final int TITLE_INDEX = 0; @Nullable public static WikivoyageContentItem parseJsonContents(String contentsJson) { @@ -25,7 +29,7 @@ public class WikivoyageJsonParser { reader = new JSONObject(contentsJson); jArray = reader.getJSONObject(HEADERS); } catch (JSONException e) { - e.printStackTrace(); + Log.e(TAG, e.getMessage(), e); return null; } @@ -37,22 +41,23 @@ public class WikivoyageJsonParser { WikivoyageContentItem headerItem = new WikivoyageContentItem(jArray.names().getString(i), link, topContentItem); topContentItem.subItems.add(headerItem); - JSONArray subheaders = header.getJSONArray(SUBHEADERS); + JSONObject subheaders = header.getJSONObject(SUBHEADERS); + JSONArray subNames = subheaders.names(); List subheaderNames = null; for (int j = 0; j < subheaders.length(); j++) { - JSONObject subheader = subheaders.getJSONObject(j); - JSONObject subheaderLink = subheader.getJSONObject(subheader.keys().next()); + String title = subNames.get(j).toString(); + JSONObject subheaderLink = subheaders.getJSONObject(title); if (subheaderNames == null) { subheaderNames = new ArrayList<>(); } - subheaderNames.add(subheader.keys().next()); + subheaderNames.add(title); link = subheaderLink.getString(LINK); - WikivoyageContentItem subheaderItem = new WikivoyageContentItem(subheader.names().getString(0), link, headerItem); + WikivoyageContentItem subheaderItem = new WikivoyageContentItem(title, link, headerItem); headerItem.subItems.add(subheaderItem); } } catch (JSONException e) { - e.printStackTrace(); + Log.e(TAG, e.getMessage(), e); } } return topContentItem;