我尝试从本地 mongodb 服务器连接和查询 mongodb。
在我的 android 应用程序中,我包含 mongo-2.10.1 驱动程序,代码为:
MongoClient mongoClient = null;
try {
mongoClient = new MongoClient("192.168.1.101", 27017);
} catch (UnknownHostException e) {
e.printStackTrace();
}
DB db = mongoClient.getDB("gaitroid_database");
DB db = mongoClient.getDB("gaitroid_database");
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("username", username);
DBCursor cursor = patients.find(searchQuery);
其中 192.168.1.101 是与我的电脑和安卓手机共享的 IPv4 地址。任何人都可以告诉我是错误的还是正确的连接方式?谢谢!!
LogCat 错误:
09-18 00:31:21.180: E/AndroidRuntime(31459): FATAL EXCEPTION: main
09-18 00:31:21.180: E/AndroidRuntime(31459): java.lang.IllegalStateException: _out shouldn't be null
09-18 00:31:21.180: E/AndroidRuntime(31459): at com.mongodb.DBPort.go(DBPort.java:110)
09-18 00:31:21.180: E/AndroidRuntime(31459): at com.mongodb.DBPort.call(DBPort.java:74)
09-18 00:31:21.180: E/AndroidRuntime(31459): at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:286)
09-18 00:31:21.180: E/AndroidRuntime(31459): at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
09-18 00:31:21.180: E/AndroidRuntime(31459): at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
09-18 00:31:21.180: E/AndroidRuntime(31459): at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
09-18 00:31:21.180: E/AndroidRuntime(31459): at com.mongodb.DB.getCollectionNames(DB.java:412)
09-18 00:31:21.180: E/AndroidRuntime(31459): at com.gaitroid.LoginActivity$1.onClick(LoginActivity.java:49)
09-18 00:31:21.180: E/AndroidRuntime(31459): at android.view.View.performClick(View.java:4354)
09-18 00:31:21.180: E/AndroidRuntime(31459): at android.view.View$PerformClick.run(View.java:17961)
09-18 00:31:21.180: E/AndroidRuntime(31459): at android.os.Handler.handleCallback(Handler.java:725)
09-18 00:31:21.180: E/AndroidRuntime(31459): at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 00:31:21.180: E/AndroidRuntime(31459): at android.os.Looper.loop(Looper.java:137)
09-18 00:31:21.180: E/AndroidRuntime(31459): at android.app.ActivityThread.main(ActivityThread.java:5328)
09-18 00:31:21.180: E/AndroidRuntime(31459): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 00:31:21.180: E/AndroidRuntime(31459): at java.lang.reflect.Method.invoke(Method.java:511)
09-18 00:31:21.180: E/AndroidRuntime(31459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-18 00:31:21.180: E/AndroidRuntime(31459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-18 00:31:21.180: E/AndroidRuntime(31459): at dalvik.system.NativeStart.main(Native Method)
蒙哥壳:
c:\mongodb\bin>mongo
MongoDB shell version: 2.4.5
connecting to: test
> show dbs
gaitroid_database 0.203125GB
local 0.078125GB
>