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

时间:2016-05-06 10:51:07

标签: java android json

我正在开发一个Android库,我在我的Test项目中添加了我的lib,现在我有一个错误,无法完成我的测试:

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)

这是我的傻瓜:

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'
}

这是我的代码的一部分:

   /* 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());
        }

我如何解决它?

1 个答案:

答案 0 :(得分:1)

您似乎正在导入某种没有put()方法的外部json库。

put()的JSONObject是&#34;内置&#34;对于Android,你不需要任何额外的库。

import org.json.JSONObject;

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

这样可行。