2

我在 Eclipse 中生成了 Android 项目,一切正常。我添加了一些带有事件的按钮等。现在,我正在尝试创建 Mongo 对象,但它会引发错误。你有什么解决办法吗?可以同时连接这两者吗?

日志:

07-11 12:40:17.655: E/AndroidRuntime(724): FATAL EXCEPTION: main
07-11 12:40:17.655: E/AndroidRuntime(724): java.lang.IllegalStateException: Could not execute method of the activity
07-11 12:40:17.655: E/AndroidRuntime(724):  at android.view.View$1.onClick(View.java:3591)
07-11 12:40:17.655: E/AndroidRuntime(724):  at android.view.View.performClick(View.java:4084)
07-11 12:40:17.655: E/AndroidRuntime(724):  at android.view.View$PerformClick.run(View.java:16966)
07-11 12:40:17.655: E/AndroidRuntime(724):  at android.os.Handler.handleCallback(Handler.java:615)
07-11 12:40:17.655: E/AndroidRuntime(724):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 12:40:17.655: E/AndroidRuntime(724):  at android.os.Looper.loop(Looper.java:137)
07-11 12:40:17.655: E/AndroidRuntime(724):  at android.app.ActivityThread.main(ActivityThread.java:4745)
07-11 12:40:17.655: E/AndroidRuntime(724):  at java.lang.reflect.Method.invokeNative(Native Method)
07-11 12:40:17.655: E/AndroidRuntime(724):  at java.lang.reflect.Method.invoke(Method.java:511)
07-11 12:40:17.655: E/AndroidRuntime(724):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-11 12:40:17.655: E/AndroidRuntime(724):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-11 12:40:17.655: E/AndroidRuntime(724):  at dalvik.system.NativeStart.main(Native Method)
07-11 12:40:17.655: E/AndroidRuntime(724): Caused by: java.lang.reflect.InvocationTargetException
07-11 12:40:17.655: E/AndroidRuntime(724):  at java.lang.reflect.Method.invokeNative(Native Method)
07-11 12:40:17.655: E/AndroidRuntime(724):  at java.lang.reflect.Method.invoke(Method.java:511)
07-11 12:40:17.655: E/AndroidRuntime(724):  at android.view.View$1.onClick(View.java:3586)
07-11 12:40:17.655: E/AndroidRuntime(724):  ... 11 more
07-11 12:40:17.655: E/AndroidRuntime(724): Caused by: java.lang.NoClassDefFoundError: com.mongodb.DBPortPool
07-11 12:40:17.655: E/AndroidRuntime(724):  at com.mongodb.DBPortPool$Holder.get(DBPortPool.java:58)
07-11 12:40:17.655: E/AndroidRuntime(724):  at com.mongodb.DBTCPConnector.setMasterAddress(DBTCPConnector.java:486)
07-11 12:40:17.655: E/AndroidRuntime(724):  at com.mongodb.DBTCPConnector.<init>(DBTCPConnector.java:46)
07-11 12:40:17.655: E/AndroidRuntime(724):  at com.mongodb.Mongo.<init>(Mongo.java:192)
07-11 12:40:17.655: E/AndroidRuntime(724):  at com.mongodb.Mongo.<init>(Mongo.java:176)
07-11 12:40:17.655: E/AndroidRuntime(724):  at com.mongodb.Mongo.<init>(Mongo.java:130)
07-11 12:40:17.655: E/AndroidRuntime(724):  at com.example.easyrest.MainActivity.clickAddOfferButton_offer(MainActivity.java:66)
07-11 12:40:17.655: E/AndroidRuntime(724):  ... 14 more
4

3 回答 3

4

此驱动程序不支持 Android。jira中有一个未解决的问题。

于 2012-07-11T13:05:08.330 回答
3

我知道这是一篇旧文章,但我只是想分享我的 Android + MongoDB 驱动程序经验。

首先,它有效!

事实证明,parvin 提到的问题已经修复并关闭。

但是,当您使用最新版本(撰写本文时为 2.11.3)时,您会看到一个非常可怕的错误正在自我呈现,但不用担心 - 驱动程序仍然可以工作。至少我能够进行身份验证并插入数据。

您将看到的错误如下所示:

I/dalvikvm(17945): Could not find method java.lang.management.ManagementFactory.getRuntimeMXBean, referenced from method org.bson.types.ObjectId.<clinit>
W/dalvikvm(17945): VFY: unable to resolve static method 7096: Ljava/lang/management/ManagementFactory;.getRuntimeMXBean ()Ljava/lang/management/RuntimeMXBean;
D/dalvikvm(17945): VFY: replacing opcode 0x71 at 0x0071
I/dalvikvm(17945): Could not find method java.lang.management.ManagementFactory.getPlatformMBeanServer, referenced from method com.mongodb.util.management.jmx.JMXMBeanServer.<init>
W/dalvikvm(17945): VFY: unable to resolve static method 7095: Ljava/lang/management/ManagementFactory;.getPlatformMBeanServer ()Ljavax/management/MBeanServer;
D/dalvikvm(17945): VFY: replacing opcode 0x71 at 0x0003
E/dalvikvm(17945): Could not find class 'javax.management.ObjectName', referenced from method com.mongodb.util.management.jmx.JMXMBeanServer.createObjectName
... etc. more scary stuff ...

我的工作代码如下所示:

MongoClient mongo = new MongoClient(ip, port);
DB db = mongo.getDB("myDatabase");
boolean auth = db.authenticate(username, password);
if (auth)
{
     DBCollection collection = db.getCollection("myCollection");
     DBObject jsonData = (DBObject) JSON.parse(jsonString);
     collection.insert(jsonData);
}

我希望这对某人有所帮助,因为我对这个错误感到非常困惑。

于 2013-11-01T01:24:46.187 回答
1

我同意 Phil 的观点,它确实有效,但它似乎会断断续续,如果您需要查找更多最新库,请查看以下内容

对于那些想要最新的预编译 JAR 文件的人 最新 Java 驱动程序的快照

如果您想要源代码,请查看GIT Source Repository 和指向最新稳定等的链接

于 2013-12-26T19:36:24.893 回答