简体   繁体   中英

Android java http Xml conversion to Json Caused by: java.lang.NoSuchMethodError: No direct method <init>(Ljava/io/Reader;)

I have to convert an Xml file at a very heavy http into a Json file.

I tried to use: implementation 'org.json: json: 20180813'

But it gives me the following error:

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)

If I go to the build.gradle file, on the lib in question it tells me:

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.

So I tried to use: Link

But it does not work.

Can you tell me where I'm wrong? Or a solution.

Code 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);
        }

    }

I had to bring in org.json lib as source files to change the package name to get it to stop conflicting with the org.json packaged with Android.

If you don't want to hand edit the package names, you might be able to use something like jarjar

Just remove

implementation 'org.json: json: 20180813'

from your build.gradle .

Everything you're using in your code is, as the warning states, already provided by Android; the library is useless at best and harmful at worst (as you can see with the runtime errors).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM