mongodb android,如何在Android中连接到MongoDB数据库?

compile 'org.mongodb:mongo-java-driver:2.13.2'

我跑我的Android应用摇篮粉碎后。 如果我直接添加罐子,我的应用程序无论如何都会粉碎。

连接下面的代码:

MongoClient mongoClient = new MongoClient("mongodb://Dbuser:[email protected]:47692");

DB db = mongoClient.getDB("testdb");

Set collectionNames = db.getCollectionNames();

所有我需要的是从我的应用程序连接到数据库蒙戈。 我不确定我的连接字符串的正确性。 在我的数据库帐户,我发现:MongoDB的:

要使用通过标准的URI连接驱动器// blablauser:[email protected]:47692/urdb

但在代码它不连接。

我使用mongo-java-driver。但每次我尝试甚至阅读我的分贝时,我得到

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.artem.mongodb/com.example.artem.mongodb.MainActivity}: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=ds047692.mongolab.com:47692, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketException: socket failed: EACCES (Permission denied)}, caused by {android.system.ErrnoException: socket failed: EACCES (Permission denied)}}]

Document myDoc = collection.find().first();//crash point

为什么我有我自己的数据库不能访问?

2015-07-19

TooLazy

+0

通过创建一个API层作为您的应用程序的“服务”来连接并处理来自数据库层的数据。 **从不**连接删除客户直接到数据库。很多安全问题,很多性能优化问题。使用API​​。 –

+0

[从Android应用程序连接MongoDB]的可能重复(http://stackoverflow.com/questions/21554887/connecting-mongodb-from-android-app) –

+0

你打算部署该应用程序给公众吗?那么这是一个非常非常糟糕的主意。最好在数据库和互联网之间放置一个应用服务器,以正确执行用户可以或不可以对数据库执行的操作。 –

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值