我尝试从我的localhost 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是与我的电脑和Android手机共享的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)
Mongo shell:
c:\mongodb\bin>mongo
MongoDB shell version: 2.4.5
connecting to: test
> show dbs
gaitroid_database 0.203125GB
local 0.078125GB
>