java - Android java http Xml 转换为 Json 引起:java.lang.NoSuchMethodError: No direct method(Ljava/io/Reader;)
问题描述
我必须将一个非常重的 http 的 Xml 文件转换为 Json 文件。
我尝试使用:implementation 'org.json: json: 20180813'
但它给了我以下错误:
Caused by: java.lang.NoSuchMethodError: No direct method <init> (Ljava / me / Reader;) V in class Lorg / json / JSONTokener; or its super classes (declaration of 'org.json.JSONTokener' appears in /system/framework/core-libart.jar)
at org.json.XMLTokener. <init> (XMLTokener.java:57)
at org.json.XML.toJSONObject (XML.java:516)
at org.json.XML.toJSONObject (XML.java:548)
at org.json.XML.toJSONObject (XML.java:472)
如果我转到 build.gradle 文件,在有问题的库上它会告诉我:
json defines classes that conflict with classes now provided by Android. Solutions includes finding newer versions or alternative libraries that do not have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar.
所以我尝试使用:链接
但它不起作用。
你能告诉我哪里错了吗?或者一个解决方案。
代码http:
private class Html extends AsyncTask<Void, Void, Void> {
private String url = "https://pastebin.com/raw/ExP2Mm2k";
private JSONArray listCh = new JSONArray();
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
Log.v("Class:" + TAG, "doInBackground:" + url);
String jsonString = Jsoup.connect(url).execute().body();
//Log.v("Class:" + TAG, "" + jsonString);
/* try {
JSONObject xmlJSONObj = XML.toJSONObject(jsonString);
String jsonPrettyPrintString = xmlJSONObj.toString(4);
System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}*/
try {
JSONObject json = new JSONObject(jsonString);
JSONArray list = json.getJSONObject("list").getJSONArray("item");
//Log.v("Class:" + TAG, String.valueOf(listCh));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
populateListItem(listCh);
}
}
解决方案
我必须将 org.json lib 作为源文件引入以更改包名称,以使其停止与 Android 打包的 org.json 冲突。
如果您不想手动编辑包名称,您可以使用类似jarjar
推荐阅读
- python - 如何使用 - 在 Python 中使用切片?
- google-sheets - 我有一个字符串列表,我想为列表中的每个项目调用多行的 INSERTDATA。我怎样才能做到这一点?
- javascript - Javascript Regex:在这种情况下如何避免灾难性的回溯
- html - 当我给附近的段落边距时,为什么我的图像会移动?
- python - 工人的 Heroku Procfile:python
- python - 如何在 python pandas 中读取包含字典的 .csv 文件?
- ios - UIButton 标题标签文本未使用情节提要或以编程方式更改
- javascript - Gatsby 外部 JS 文件中的构造函数中的变量
- c - 采用 char 数组和 2 个索引的函数;交换这些索引中的字符
- sql-server - 如何通过报表管理器从可用值中设置默认值