首页 > 解决方案 > 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);
        }

    }

标签: javaandroidjsonxml

解决方案


我必须将 org.json lib 作为源文件引入以更改包名称,以使其停止与 Android 打包的 org.json 冲突。

如果您不想手动编辑包名称,您可以使用类似jarjar


推荐阅读