2

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 ?

2
  • post your tr object where you set the values please Commented May 6, 2016 at 10:53
  • Looks like incompatible classes Commented May 6, 2016 at 10:55

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have imported a external library because in the process of exportation of my jar I have a problem with JSON function (cannot resolve Json...)
@Max, well, the library you use does not support put() method on the json object. You can either use the builtin json library like I showed, or find a corresponding method for put() in your external library
Ok, how i can resove this problem ?
I don't undestard, if i wrote a class in Android app all my code work perfectlym but If i create a java lib does't work because "JSON* cannot resolve symbol"
1

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)

Comments

0

Per the documentation, the Object value passed to JSONObject.put must be:

...a JSONObject, JSONArray, String, Boolean, Integer, Long, Double, NULL, or null. May not be NaNs or infinities.

tr.getValue() is of type ArrayList<String>. Since this is not one of the listed types, the attempt fails at runtime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.