自2.9.0(link)起,Android上应支持MongoDB。我已经下载了2.11.3 MongoDB java驱动程序并设置了一个简单的代码:
MongoClient mongo = new MongoClient(hostname, port);
db = mongo.getDB(dbname);
CommandResult lastError = db.getLastError();
if (!lastError.ok()) {
throw new ConnectException("Error connecting to MongoDB: " + lastError.getErrorMessage());
}
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
Log.w("sd", s);
}
但是,当我跑步时,我会收到消息:
01-18 19:47:39.173: I/dalvikvm(25567): Could not find method java.lang.management.ManagementFactory.getRuntimeMXBean, referenced from method org.bson.types.ObjectId.<clinit>
01-18 19:47:39.183: W/dalvikvm(25567): VFY: unable to resolve static method 6785: Ljava/lang/management/ManagementFactory;.getRuntimeMXBean ()Ljava/lang/management/RuntimeMXBean;
01-18 19:47:39.183: D/dalvikvm(25567): VFY: replacing opcode 0x71 at 0x0071
01-18 19:47:39.293: I/dalvikvm(25567): Could not find method java.lang.management.ManagementFactory.getPlatformMBeanServer, referenced from method com.mongodb.util.management.jmx.JMXMBeanServer.<init>
01-18 19:47:39.293: W/dalvikvm(25567): VFY: unable to resolve static method 6784: Ljava/lang/management/ManagementFactory;.getPlatformMBeanServer ()Ljavax/management/MBeanServer;
01-18 19:47:39.293: D/dalvikvm(25567): VFY: replacing opcode 0x71 at 0x0003
01-18 19:47:39.293: E/dalvikvm(25567): Could not find class 'javax.management.ObjectName', referenced from method com.mongodb.util.management.jmx.JMXMBeanServer.createObjectName
01-18 19:47:39.293: W/dalvikvm(25567): VFY: unable to resolve new-instance 1058 (Ljavax/management/ObjectName;) in Lcom/mongodb/util/management/jmx/JMXMBeanServer;
01-18 19:47:39.293: D/dalvikvm(25567): VFY: replacing opcode 0x22 at 0x0000
01-18 19:47:39.303: W/dalvikvm(25567): VFY: unable to resolve exception class 1055 (Ljavax/management/MalformedObjectNameException;)
01-18 19:47:39.303: W/dalvikvm(25567): VFY: unable to find exception handler at addr 0x6
01-18 19:47:39.303: W/dalvikvm(25567): VFY: rejected Lcom/mongodb/util/management/jmx/JMXMBeanServer;.createObjectName (Ljava/lang/String;)Ljavax/management/ObjectName;
01-18 19:47:39.303: W/dalvikvm(25567): VFY: rejecting opcode 0x0d at 0x0006
01-18 19:47:39.303: W/dalvikvm(25567): VFY: rejected Lcom/mongodb/util/management/jmx/JMXMBeanServer;.createObjectName (Ljava/lang/String;)Ljavax/management/ObjectName;
01-18 19:47:39.303: W/dalvikvm(25567): Verifier rejected class Lcom/mongodb/util/management/jmx/JMXMBeanServer;
以及db.getLastError()
中的NetworkOnMainThreadException。