'Java.lang.NoSuchMethodError: org.json.JSONObject.put

I'm developing an Android library, i add my lib in my Test project, now i have a error that can't allow to finish my test:

Could not find method org.json.JSONObject.put, referenced from method com.ldm.JsonBuilder.jsonbuilder

Process: , PID: 31181
java.lang.NoSuchMethodError: org.json.JSONObject.put
at com.ldm.JsonBuilder.jsonbuilder(JsonBuilder.java:70)
at com.ldm.TestConnection.TestNow(TestConnection.java:79)
at ldm.eisworld.it.test.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5442)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)

This is my gradle:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('libs/httpcore-4.4.4.jar')
    compile files('libs/json.jar')
    compile 'org.json:json:20141113'
}

And this is a part of my code:

   /* Parsing of hash map */
    for (Map.Entry<String, ArrayList<String>> tr : rh.entrySet()) {

        try {
            /* Object that contains URL */
            JSONObject TraceUrl = new JSONObject();
            /* Object that contains nodes for each URL */
            JSONObject TraceNodes = new JSONObject();

            TraceUrl.put("url", tr.getKey());
            TraceUrl.put("nodes", tr.getValue());
            trace.put(TraceUrl);
        } catch (JSONException e) {
            System.out.println("Exception: " + e.toString());
        }

How i can resolve it ?



Solution 1:[1]

You appear to be importing some kind of external json library that doesn't have a put() method.

JSONObject that has put() is "built in" to android, you don't need any additional libraries for it.

import org.json.JSONObject;

JSONObject jsonObject = new JSONObject();
jsonObject.put("key", "value");

This will work.

Solution 2:[2]

KOTLIN
Late but, i think it might help someone. i had this error

No virtual method put(Ljava/lang/String;F)Lorg/json/JSONObject; in class Lorg/json/JSONObject

i was importing the correct class and not importing any other related 3rd party libs. even added progaurd rules but nothing helped.

then i replaced

                val orderRequest = JSONObject()
                orderRequest.put("amount", amount)
                orderRequest.put("currency", "INR")
                orderRequest.put("receipt", "order_rcptid_11")

with

         val reqStr = """
                    {
                    "amount" : "$amount",
                    "currency" : "INR",
                    "receipt" : "order_rcptid_11"
                    }
                    """.trimIndent()
                
         val orderRequest = JSONObject(reqStr)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim
Solution 2 Nasib