0

i´m having trouble with a little android app i developed with Android Studio. All i like to do is to get the JSON out of an xml string using org.json.XML in a button click routine. But unfortunately the app crashes in debug mode without an error, when i run the app normally, it generates the following error messages:

              --------- beginning of crash

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.drudo.anothertestapp, PID: 3068 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5198)  at android.view.View$PerformClick.run(View.java:21147)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  Caused by: java.lang.NoSuchMethodError: No static method stringToValue(Ljava/lang/String;)Ljava/lang/Object; in class Lorg/json/JSONObject; or its super classes (declaration of 'org.json.JSONObject' appears in /system/framework/core-libart.jar) at org.json.XML.parse(XML.java:257) at org.json.XML.parse(XML.java:263) at org.json.XML.toJSONObject(XML.java:302) at com.example.drudo.anothertestapp.MainActivity.onButtonClick(MainActivity.java:28) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  at android.view.View.performClick(View.java:5198)  at android.view.View$PerformClick.run(View.java:21147)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  W/art: Suspending all threads took: 8.734ms I/Process: Sending signal. PID: 3068 SIG: 9 Application terminated.

onButtonClick Code:

 public void onButtonClick(View view)
{
    String XML_TESTSTRING = "<item><value>test</value></item>";

    JSONObject jsonObject = null;

    try
    {
        jsonObject = XML.toJSONObject(XML_TESTSTRING);//here crash
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
}

The json library i included in the project is the following: https://mvnrepository.com/artifact/org.json/json/1.5-20090211

I created a libs directory in app, copied the jar to it and right-click 'Add as Library'. My dependencies part in build.gradle looks as follows:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.android.gms:play-services-appindexing:8.1.0'
compile files('libs/json-1.5-20090211.jar')

}

My best guess is that the problem is regarding the Android api included org.json and the one i downloaded and included, but i have no clue about how to proceed. Any suggestions?

Zoe
  • 27,060
  • 21
  • 118
  • 148
GinSonic
  • 70
  • 9
  • I encountered such an error. Could be an error in the inbuilt api. I used the original json.org implementation jar – Zuko Oct 20 '16 at 14:20
  • @GinSonic have you tried to put valid json string inside? – vchornenyi Oct 20 '16 at 14:27
  • the library is trying to access an method that not exists: `No static method stringToValue(Ljava/lang/String;)Ljava/lang/Object; in class Lorg/json/JSONObject; or its super`, maybe you are missing one dependency of the library – Lucas Queiroz Ribeiro Oct 20 '16 at 14:31
  • @temnoi. I think so. The json string i also tested was: { "CookieText": "A friend asks only for your time not your money."}. Same result, the app crashed there. – GinSonic Oct 20 '16 at 14:38
  • @LucasQueirozRibeiro Good point! What i now figured out is that according to http://static.javadoc.io/org.json/json/20160212/org/json/XML.html the stringToValue(java.lang.String string) is deprecated and should be replaced with JSONObject.stringToValue(String) instead. But how do i achieve this? – GinSonic Oct 20 '16 at 14:45
  • You don't, the library owner may implement that, or if the library is open source, you can implement by your own and submit a pull request – Lucas Queiroz Ribeiro Oct 20 '16 at 15:09
  • I found in [other question](http://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java) a xml with header, maybe can be it : `Turn this to JSON";` – Lucas Queiroz Ribeiro Oct 20 '16 at 15:11

1 Answers1

1

There is conflict between the Android framework's org.json and the repository org.json we add externally. Android doesn't have the required method toJsonObject(testString) in XML class.

Utilise this open source

https://github.com/stleary/JSON-java You should refactor this package and proceed.

See to https://github.com/stleary/JSON-java/wiki/JSON-Java-for-Android-developers for more details described the developer.

I tried this and this is working perfect.

LincyTonita
  • 327
  • 4
  • 14