如何将Mongodb与Android应用程序连接起来

时间:2012-07-11 12:52:12

标签: java android mongodb

我在Eclipse中生成了Android项目,一切正常。我添加了几个按钮等事件。现在,我正在尝试创建Mongo对象,但它会抛出错误。你有任何解决方案吗?是否可以连接这两个?

LOG:

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

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

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

首先,IT工作!

事实证明,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);
}

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

答案 2 :(得分:1)

我同意Phil它确实有效,但它似乎再次突破,如果您需要找到更多最新的库,请查看以下内容

对于那些需要最新的预编译JAR文件Snapshots of latest Java Drivers

的人

如果您需要来源,请查看GIT Source Repository and links to latest stable etc