Contents json parsing fix

This commit is contained in:
PaulStets 2018-04-14 16:38:06 +03:00
parent aad0e5b5e1
commit 60c3cae47d
2 changed files with 26 additions and 8 deletions

View file

@ -49,7 +49,20 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
"<meta http-equiv=\"cleartype\" content=\"on\" />\n" +
"<link href=\"article_style.css\" type=\"text/css\" rel=\"stylesheet\"/>\n" +
"</head><body>\n" + "<script>" + "function scrollAnchor(id) {" +
"window.location.hash = id;}</script>";
"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\";" +
" }" +
" });" +
"}</script>";
private static final String FOOTER_INNER = "</div></body></html>";
private long cityId = NO_VALUE;

View file

@ -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<String> 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;