我用Java Swing和MongoDB制作了一个小应用程序。
我正在尝试为Android开发相同的应用程序,当我尝试连接时,与MongoDB数据库的连接会导致连接超时。
我尝试使用带有3G访问权限的三星应用程序以及可访问互联网的Android虚拟设备。
我使用MongoLab数据库,与我的Java Swing应用程序相同的代码和相同的驱动程序。
//DB
MongoClientURI uri = new MongoClientURI("mongodb://user:password@ds033750.mongolab.com:33750/db");
MongoClient mongo = null;
try {
mongo = new MongoClient(uri);
}
catch (Throwable e) {
e.printStackTrace();
}
DB db = mongo.getDB(uri.getDatabase());
DBCollection imagesCollection = db.getCollection("images");
DBCursor cursor = imagesCollection.find();
try {
while(cursor.hasNext()) {
DBObject obj = cursor.next();
Log.d("test", (String) obj.get("name"));
}
} finally {
cursor.close();
}
这是错误:
com.mongodb.MongoTimeoutException: Timed out while waiting to connect after 10000 ms
at com.mongodb.BaseCluster.getDescription(BaseCluster.java:131)
at com.mongodb.DBTCPConnector.getClusterDescription(DBTCPConnector.java:396)
at com.mongodb.DBTCPConnector.getType(DBTCPConnector.java:569)
at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:370)
at com.mongodb.Mongo.isMongosConnection(Mongo.java:645)
at com.mongodb.DBCursor._check(DBCursor.java:454)
at com.mongodb.DBCursor._hasNext(DBCursor.java:546)
at com.mongodb.DBCursor.hasNext(DBCursor.java:571)
at com.leafstore.MainActivity.onCreate(MainActivity.java:100)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
完整日志:
09-05 15:54:41.569: I/dalvikvm(10076): Could not find method java.lang.management.ManagementFactory.getRuntimeMXBean, referenced from method org.bson.types.ObjectId.<clinit>
09-05 15:54:41.569: W/dalvikvm(10076): VFY: unable to resolve static method 12702: Ljava/lang/management/ManagementFactory;.getRuntimeMXBean ()Ljava/lang/management/RuntimeMXBean;
09-05 15:54:41.569: D/dalvikvm(10076): VFY: replacing opcode 0x71 at 0x0071
09-05 15:54:41.569: W/org.bson.ObjectId(10076): null
09-05 15:54:41.569: W/org.bson.ObjectId(10076): java.net.SocketException
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at java.net.NetworkInterface.rethrowAsSocketException(NetworkInterface.java:224)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at java.net.NetworkInterface.collectIpv4Address(NetworkInterface.java:190)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at java.net.NetworkInterface.getByName(NetworkInterface.java:118)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at java.net.NetworkInterface.getNetworkInterfacesList(NetworkInterface.java:283)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at java.net.NetworkInterface.getNetworkInterfaces(NetworkInterface.java:274)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at org.bson.types.ObjectId.<clinit>(ObjectId.java:548)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at com.mongodb.Bytes.<clinit>(Bytes.java:226)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at com.mongodb.Mongo.<clinit>(Mongo.java:76)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at com.leafstore.MainActivity.onCreate(MainActivity.java:91)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at android.app.Activity.performCreate(Activity.java:5372)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at android.app.ActivityThread.access$700(ActivityThread.java:159)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at android.os.Looper.loop(Looper.java:137)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at android.app.ActivityThread.main(ActivityThread.java:5419)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at java.lang.reflect.Method.invoke(Method.java:525)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at dalvik.system.NativeStart.main(Native Method)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at libcore.io.Posix.socket(Native Method)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): at java.net.NetworkInterface.collectIpv4Address(NetworkInterface.java:175)
09-05 15:54:41.569: W/org.bson.ObjectId(10076): ... 21 more
09-05 15:54:41.589: I/dalvikvm(10076): Could not find method java.lang.management.ManagementFactory.getPlatformMBeanServer, referenced from method com.mongodb.util.management.jmx.JMXMBeanServer.<init>
09-05 15:54:41.589: W/dalvikvm(10076): VFY: unable to resolve static method 12701: Ljava/lang/management/ManagementFactory;.getPlatformMBeanServer ()Ljavax/management/MBeanServer;
09-05 15:54:41.589: D/dalvikvm(10076): VFY: replacing opcode 0x71 at 0x0003
09-05 15:54:41.589: E/dalvikvm(10076): Could not find class 'javax.management.ObjectName', referenced from method com.mongodb.util.management.jmx.JMXMBeanServer.createObjectName
09-05 15:54:41.589: W/dalvikvm(10076): VFY: unable to resolve new-instance 1732 (Ljavax/management/ObjectName;) in Lcom/mongodb/util/management/jmx/JMXMBeanServer;
09-05 15:54:41.589: D/dalvikvm(10076): VFY: replacing opcode 0x22 at 0x0000
09-05 15:54:41.589: W/dalvikvm(10076): VFY: unable to find class referenced in signature (Ljavax/management/ObjectName;)
09-05 15:54:41.589: I/dalvikvm(10076): Could not find method javax.management.MBeanServer.isRegistered, referenced from method com.mongodb.util.management.jmx.JMXMBeanServer.isRegistered
09-05 15:54:41.589: W/dalvikvm(10076): VFY: unable to resolve interface method 13063: Ljavax/management/MBeanServer;.isRegistered (Ljavax/management/ObjectName;)Z
09-05 15:54:41.589: D/dalvikvm(10076): VFY: replacing opcode 0x72 at 0x0006
09-05 15:54:41.589: W/dalvikvm(10076): VFY: unable to resolve exception class 1730 (Ljavax/management/MalformedObjectNameException;)
09-05 15:54:41.589: W/dalvikvm(10076): VFY: unable to find exception handler at addr 0xb
09-05 15:54:41.589: W/dalvikvm(10076): VFY: rejected Lcom/mongodb/util/management/jmx/JMXMBeanServer;.isRegistered (Ljava/lang/String;)Z
09-05 15:54:41.589: W/dalvikvm(10076): VFY: rejecting opcode 0x0d at 0x000b
09-05 15:54:41.589: W/dalvikvm(10076): VFY: rejected Lcom/mongodb/util/management/jmx/JMXMBeanServer;.isRegistered (Ljava/lang/String;)Z
09-05 15:54:41.589: W/dalvikvm(10076): Verifier rejected class Lcom/mongodb/util/management/jmx/JMXMBeanServer;
09-05 15:54:51.598: W/System.err(10076): com.mongodb.MongoTimeoutException: Timed out while waiting to connect after 10000 ms
09-05 15:54:51.598: W/System.err(10076): at com.mongodb.BaseCluster.getDescription(BaseCluster.java:131)
09-05 15:54:51.608: W/System.err(10076): at com.mongodb.DBTCPConnector.getClusterDescription(DBTCPConnector.java:396)
09-05 15:54:51.608: W/System.err(10076): at com.mongodb.DBTCPConnector.getType(DBTCPConnector.java:569)
09-05 15:54:51.608: W/System.err(10076): at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:370)
09-05 15:54:51.608: W/System.err(10076): at com.mongodb.Mongo.isMongosConnection(Mongo.java:645)
09-05 15:54:51.608: W/System.err(10076): at com.mongodb.DBCursor._check(DBCursor.java:454)
09-05 15:54:51.608: W/System.err(10076): at com.mongodb.DBCursor._hasNext(DBCursor.java:546)
09-05 15:54:51.618: W/System.err(10076): at com.mongodb.DBCursor.hasNext(DBCursor.java:571)
09-05 15:54:51.618: W/System.err(10076): at com.leafstore.MainActivity.onCreate(MainActivity.java:100)
09-05 15:54:51.618: W/System.err(10076): at android.app.Activity.performCreate(Activity.java:5372)
09-05 15:54:51.618: W/System.err(10076): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
09-05 15:54:51.618: W/System.err(10076): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
09-05 15:54:51.628: W/System.err(10076): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
09-05 15:54:51.628: W/System.err(10076): at android.app.ActivityThread.access$700(ActivityThread.java:159)
09-05 15:54:51.628: W/System.err(10076): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
09-05 15:54:51.628: W/System.err(10076): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 15:54:51.628: W/System.err(10076): at android.os.Looper.loop(Looper.java:137)
09-05 15:54:51.628: W/System.err(10076): at android.app.ActivityThread.main(ActivityThread.java:5419)
09-05 15:54:51.638: W/System.err(10076): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 15:54:51.638: W/System.err(10076): at java.lang.reflect.Method.invoke(Method.java:525)
09-05 15:54:51.638: W/System.err(10076): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
09-05 15:54:51.638: W/System.err(10076): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
09-05 15:54:51.648: W/System.err(10076): at dalvik.system.NativeStart.main(Native Method)
09-05 15:54:51.839: D/libEGL(10076): loaded /vendor/lib/egl/libEGL_adreno.so
09-05 15:54:51.849: D/libEGL(10076): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
09-05 15:54:51.859: D/libEGL(10076): loaded /vendor/lib/egl/libGLESv2_adreno.so
09-05 15:54:51.869: I/Adreno-EGL(10076): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL4169980)
09-05 15:54:51.869: I/Adreno-EGL(10076): OpenGL ES Shader Compiler Version: 17.01.10.SPL
09-05 15:54:51.869: I/Adreno-EGL(10076): Build Date: 09/26/13 Thu
09-05 15:54:51.869: I/Adreno-EGL(10076): Local Branch:
09-05 15:54:51.869: I/Adreno-EGL(10076): Remote Branch:
09-05 15:54:51.869: I/Adreno-EGL(10076): Local Patches:
09-05 15:54:51.869: I/Adreno-EGL(10076): Reconstruct Branch:
09-05 15:54:52.129: D/OpenGLRenderer(10076): Enabling debug mode 0
09-05 15:54:52.159: D/OpenGLRenderer(10076): GL error from OpenGLRenderer: 0x502
09-05 15:54:52.159: E/OpenGLRenderer(10076): GL_INVALID_OPERATION