即使使用AsyncTask,Android也无法连接到MongoDB

第555章
public class StartNewActivity extends Activity{

public static final String TEST_SKETCH_APP = "testSketchApp";
public static final String SKETCH_COLLECTION = "sketchCollection";
private EditText editText;
private EditText editText2;
private EditText editText3;
private EditText editText4;

private String name;
private String password;
private String email;
private String phoneNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);
    editText = (EditText) findViewById(R.id.editText);
    editText2 = (EditText) findViewById(R.id.editText2);
    editText3 = (EditText) findViewById(R.id.editText3);
    editText4 = (EditText) findViewById(R.id.editText4);

}

@Override
public void onBackPressed() {
        Intent mainActivityIntent = new Intent(StartNewActivity.this, MainActivity.class);
        startActivity(mainActivityIntent);
        super.onBackPressed();

}

public void onSubmitButton(View view){

    name = editText.getText().toString();
    password = editText2.getText().toString();
    email = editText3.getText().toString();
    phoneNumber = editText4.getText().toString();


    // Gets the URL from the UI's text field.
    ConnectivityManager connMgr = (ConnectivityManager)
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        new MakeDataBaseConnection().execute(name, password, email, phoneNumber);
    } else {
        final Toast toast = Toast.makeText(getApplicationContext(), "No Network available", Toast.LENGTH_SHORT);
        toast.show();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, 500);
    }

}



private class MakeDataBaseConnection extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        // params comes from the execute() call: params[0] is the url.
        return saveValues(urls);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        final Toast toast = Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT);
        toast.show();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, 500);
    }
}

private String saveValues(String[] url) {

    try {

        MongoClientURI uri = new MongoClientURI("mongodb://localhost:27017");

        MongoClient instance = MongoDatabaseConnection.getInstance(uri);
        DB db = instance.getDB(TEST_SKETCH_APP);
        DBCollection collection = db.getCollection(SKETCH_COLLECTION);

        BasicDBObject  dbObject = new BasicDBObject();

        dbObject.put("name", url[0]);
        dbObject.put("password", url[1]);
        dbObject.put("email", url[2]);
        dbObject.put("phoneNumber", url[3]);

        collection.insert(dbObject);

        return "Values Saved";
    }catch (Exception e){
        return "Not Saved, "+e.getMessage();
    }

}
}

我正在尝试将值保存在MongoDB数据库中,我的数据库肯定在后台运行。但是,当我尝试插入Document对象时,它会引发如下异常:

com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of 
cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: failed 
to connect to localhost/127.0.0.1 (port 27017) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)}, caused by {android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)}}]

我正在使用WorkerThread按建议保存操作,并也使用AsyncTask进行数据库操作,但是没有打开数据库套接字。你能让我知道我在想什么吗?或显示一个示例,说明如何通过网络连接到数据库。

乔纳斯·柯瑞兹(JonasKöritz)

我不认为MongoDB在您的android设备上运行,与localhost的连接将超时,因为没有MongoDB Server在127.0.0.1上运行。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

即使可以连接也无法连接到主机

来自分类Dev

即使使用“信任”设置也无法连接到 postgres 数据库

来自分类Dev

即使在创建用户后也无法远程连接到 mySQL

来自分类Dev

苹果公司说即使连接了wifi也无法连接到wifi

来自分类Dev

即使经过全新安装,Ubuntu 14.04也无法或无法连接到wifi

来自分类Dev

使用asynctask android连接到套接字

来自分类Dev

即使提供正确的mysql服务名称,Spring-boot也无法连接到mysql容器

来自分类Dev

为什么即使连接到网络也无法访问互联网?

来自分类Dev

即使 docker-compose up 成功,也无法连接到 docker 端点

来自分类Dev

在openshift上使用socket.io和node.js时,即使指定了端口8000,也无法连接到端口8080错误

来自分类Dev

即使在强制使用 IPv4、清除 DNS 缓存、更改镜像后,apt update 也无法连接到 archive.ubuntu.com

来自分类Dev

即使使用Makefile也无法使用Makefile链接到SFML库

来自分类Dev

对于使用PyBluez的蓝牙连接,即使配对后也无法接受套接字连接

来自分类Dev

无法使用机器ip连接到mongodb

来自分类Dev

无法使用Mongoose连接到MongoDB

来自分类Dev

无法使用MongoClient连接到mongoDB

来自分类Dev

将WSP部署到SP2013:即使正确部署了WSP,我也无法连接到sql数据

来自分类Dev

即使打开并连接到互联网,计算机也无法节省时间

来自分类Dev

即使在将公钥手动添加到 authorized_keys 后,也无法连接到 AWS 实例

来自分类Dev

即使 rsa_id.pub 已插入到授权密钥,也无法通过 ssh 连接到服务器

来自分类Dev

即使 Postgres 正在运行并且凭据正确,mix ecto.create 也无法连接到 Postgres

来自分类Dev

如何使用AsyncTask连接到Android中的网络

来自分类Dev

为什么即使使用HTML5也无法通过浏览器进行UDP连接?

来自分类Dev

即使在ASP.net中打开了连接,ExecuteScalar也无法使用

来自分类Dev

即使使用ScrollPhysics也无法滚动ListView

来自分类Dev

无法使用连接字符串连接到MongoDB

来自分类Dev

即使索引也无法连接两个表MySQL

来自分类Dev

即使已连接 PHP 也无法选择数据库

来自分类Dev

连接被拒绝:即使增加了连接扩展层,服务器也无法在Solace代理中使用

Related 相关文章

  1. 1

    即使可以连接也无法连接到主机

  2. 2

    即使使用“信任”设置也无法连接到 postgres 数据库

  3. 3

    即使在创建用户后也无法远程连接到 mySQL

  4. 4

    苹果公司说即使连接了wifi也无法连接到wifi

  5. 5

    即使经过全新安装,Ubuntu 14.04也无法或无法连接到wifi

  6. 6

    使用asynctask android连接到套接字

  7. 7

    即使提供正确的mysql服务名称,Spring-boot也无法连接到mysql容器

  8. 8

    为什么即使连接到网络也无法访问互联网?

  9. 9

    即使 docker-compose up 成功,也无法连接到 docker 端点

  10. 10

    在openshift上使用socket.io和node.js时,即使指定了端口8000,也无法连接到端口8080错误

  11. 11

    即使在强制使用 IPv4、清除 DNS 缓存、更改镜像后,apt update 也无法连接到 archive.ubuntu.com

  12. 12

    即使使用Makefile也无法使用Makefile链接到SFML库

  13. 13

    对于使用PyBluez的蓝牙连接,即使配对后也无法接受套接字连接

  14. 14

    无法使用机器ip连接到mongodb

  15. 15

    无法使用Mongoose连接到MongoDB

  16. 16

    无法使用MongoClient连接到mongoDB

  17. 17

    将WSP部署到SP2013:即使正确部署了WSP,我也无法连接到sql数据

  18. 18

    即使打开并连接到互联网,计算机也无法节省时间

  19. 19

    即使在将公钥手动添加到 authorized_keys 后,也无法连接到 AWS 实例

  20. 20

    即使 rsa_id.pub 已插入到授权密钥,也无法通过 ssh 连接到服务器

  21. 21

    即使 Postgres 正在运行并且凭据正确,mix ecto.create 也无法连接到 Postgres

  22. 22

    如何使用AsyncTask连接到Android中的网络

  23. 23

    为什么即使使用HTML5也无法通过浏览器进行UDP连接?

  24. 24

    即使在ASP.net中打开了连接,ExecuteScalar也无法使用

  25. 25

    即使使用ScrollPhysics也无法滚动ListView

  26. 26

    无法使用连接字符串连接到MongoDB

  27. 27

    即使索引也无法连接两个表MySQL

  28. 28

    即使已连接 PHP 也无法选择数据库

  29. 29

    连接被拒绝:即使增加了连接扩展层,服务器也无法在Solace代理中使用

热门标签

归档