我正在尝试构建 android 应用程序以连接到 MongoDB。卡在这个问题上。
MongoDB 是可访问的并且没有安全性。可以通过手机使用 Mono Explorer 添加数据。
public void sendMessage(View view) throws UnknownHostException {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("<MY IP>", 27017);
/**** Get database ****/
DB db = mongo.getDB("test");
DBCollection table = db.getCollection("test");
BasicDBObject document = new BasicDBObject();
document.put("name", "mkyong");
document.put("age", 30);
document.put("createdDate", new Date(0));
try {
table.insert(document);
} catch (NullPointerException e) {
throw new RuntimeException("unable to connect to MongoDB " + table.getFullName(), e);
}
}
日志:
11-04 19:01:56.825: E/dalvikvm(4448): Could not find class 'javax.management.ObjectName', referenced from method com.mongodb.util.management.jmx.JMXMBeanServer.createObjectName
11-04 19:01:57.095: E/AndroidRuntime(4448): FATAL EXCEPTION: main
11-04 19:01:57.095: E/AndroidRuntime(4448): java.lang.IllegalStateException: Could not execute method of the activity
11-04 19:01:57.095: E/AndroidRuntime(4448): at android.view.View$1.onClick(View.java:3691)
11-04 19:01:57.095: E/AndroidRuntime(4448): at android.view.View.performClick(View.java:4211)
11-04 19:01:57.095: E/AndroidRuntime(4448): at android.view.View$PerformClick.run(View.java:17267)
11-04 19:01:57.095: E/AndroidRuntime(4448): at android.os.Handler.handleCallback(Handler.java:615)
11-04 19:01:57.095: E/AndroidRuntime(4448): at android.os.Handler.dispatchMessage(Handler.java:92)
11-04 19:01:57.095: E/AndroidRuntime(4448): at android.os.Looper.loop(Looper.java:137)
11-04 19:01:57.095: E/AndroidRuntime(4448): at android.app.ActivityThread.main(ActivityThread.java:4898)
11-04 19:01:57.095: E/AndroidRuntime(4448): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 19:01:57.095: E/AndroidRuntime(4448): at java.lang.reflect.Method.invoke(Method.java:511)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-04 19:01:57.095: E/AndroidRuntime(4448): at dalvik.system.NativeStart.main(Native Method)
11-04 19:01:57.095: E/AndroidRuntime(4448): Caused by: java.lang.reflect.InvocationTargetException
11-04 19:01:57.095: E/AndroidRuntime(4448): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 19:01:57.095: E/AndroidRuntime(4448): at java.lang.reflect.Method.invoke(Method.java:511)
11-04 19:01:57.095: E/AndroidRuntime(4448): at android.view.View$1.onClick(View.java:3686)
11-04 19:01:57.095: E/AndroidRuntime(4448): ... 11 more
11-04 19:01:57.095: E/AndroidRuntime(4448): Caused by: java.lang.IllegalStateException: _out shouldn't be null
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.mongodb.DBPort.go(DBPort.java:110)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.mongodb.DBPort.call(DBPort.java:74)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:286)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.mongodb.DB.command(DB.java:274)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.mongodb.DB.command(DB.java:256)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.mongodb.DB.command(DB.java:313)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.mongodb.Mongo.getDatabaseNames(Mongo.java:393)
11-04 19:01:57.095: E/AndroidRuntime(4448): at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:48)
11-04 19:01:57.095: E/AndroidRuntime(4448): ... 14 more