0
votes

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?

1
I encountered such an error. Could be an error in the inbuilt api. I used the original json.org implementation jar - Zuko
@GinSonic have you tried to put valid json string inside? - temnoi
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
@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
@LucasQueirozRibeiro Good point! What i now figured out is that according to 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

1 Answers

1
votes

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.