代码连接到mongoDB错误,导入的jar无法识别

时间:2014-04-08 07:19:46

标签: java android eclipse mongodb maven

我正在尝试连接到mogoDB数据库,我按照

中的说明安装了mongoDB

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/

在ubuntu终端通过mongoshell与mongo一起玩,一切都很精明,然后我试着在Android应用程序中实现它。我参考了这个教程

http://www.mkyong.com/mongodb/java-mongodb-insert-a-document/

并编辑它以创建下面列出的代码

package com.example.test1;

import java.net.UnknownHostException;
import java.text.MessageFormat;
import java.util.Set;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.mongodb.DB;
import com.mongodb.MongoClient;

public class InsertDocumentApp extends ActionBarActivity {
private Button btnRestart;
private Button btnCancel = null;
private TextView txtMessage =  null;
private TextView txt=null;
private ProgressBar mProgressBar =  null;
private HugeWork task = null;
private static final int MAX_PROGRESS = 10;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showdata);

    btnRestart = (Button) findViewById(R.id.btnRestart);
    btnCancel = (Button) findViewById(R.id.btnCancel);
    txtMessage = (TextView) findViewById(R.id.txtMessage);
    mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    txt =  (TextView) findViewById(R.id.textViewname);
    // set an arbitrary max value for the progress bar
    mProgressBar.setMax(MAX_PROGRESS);
    // start the async task
    start();
}


public void cancelOnclick(View v) {
    task.cancel(true);
    btnCancel.setEnabled(false);
    btnRestart.setEnabled(true);
}

// Restart the process execution. This is the listener to the Restart button.
public void restartOnclick(View v) {
    start();
}


private void start() {

    task = new HugeWork();

    task.execute(0);

    mProgressBar.setProgress(0);

    btnCancel.setEnabled(true);
    btnRestart.setEnabled(false);
}


private void executeHardWork() 
{
    MongoClient mongoClient;
    DB db;
    try {
        try
        { 
        mongoClient = new MongoClient(); //connects to mongo server locally (seems to work)
        // mongoClient = new MongoClient("localhost",27017); //connects to mongo server locally (seems to work)
        mongoClient = new MongoClient("localhost",27017); // connects to mongo server locally
        db = mongoClient.getDB("mydb"); // connect to a database --implicitly creates database if none exists
        Set<String> colls = db.getCollectionNames();
        for (String s : colls)
            System.out.println(s);

        mongoClient.close();

        } catch (UnknownHostException ex) {
        ex.printStackTrace();
        }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
} 

        @Override
    protected void onPreExecute() {
        txtMessage.setText("Executing async task...");
        super.onPreExecute();
    }


    @Override
    protected Integer doInBackground(Integer... params) {


        int progress = ((Integer[])params)[0];

        do {

                            if (!this.isCancelled()) {
                // execute hard work - sleep
                executeHardWork();
            }
            else {
                                    break;
            }


            progress++;
            publishProgress(progress);
        } while (progress <= MAX_PROGRESS);

        return progress;
    }


    @Override
    protected void onProgressUpdate(Integer... values) {
        int progress = ((Integer[])values)[0];
        mProgressBar.setProgress(progress);
        super.onProgressUpdate(values);
    }


    @Override
    protected void onCancelled(Integer result) {
        txtMessage.setText(MessageFormat.format
        ("Async task has been cancelled at {0} seconds.", result - 1));
        super.onCancelled(result);
    }


    @Override
    protected void onPostExecute(Integer result) {
        txtMessage.setText(MessageFormat.format
        ("Async task execution finished in {0} seconds.", result - 1));
        btnCancel.setEnabled(false);
        btnRestart.setEnabled(true);
        super.onPostExecute(result);
    }
}

}

我注意到即使我输入了从这个链接获得的jar文件,教程中的一些导入也没有进入ADT

http://central.maven.org/maven2/org/mongodb/mongo-java-driver/2.11.3/

这是我下载的jar文件mongo-java-driver-2.11.3.jar 我通过,project / buildpath / add external jar导入它 错误随处可见“mongo”项目&#39;在那儿。 是因为eclipse没有识别我添加到构建路径的jar文件? 这是正确的jar文件吗?我需要做更多的事情来让我的应用程序运行吗?

这是在终端

中输入mongo时出现的终端提示
mukund@mukund-ThinkPad-Edge-E431:~$ mongo
MongoDB shell version: 2.4.10
connecting to: test
Server has startup warnings: 
Wed Apr  9 10:03:04.513 [initandlisten] 
Wed Apr  9 10:03:04.513 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Wed Apr  9 10:03:04.513 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal). 
Wed Apr  9 10:03:04.513 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Wed Apr  9 10:03:04.513 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Wed Apr  9 10:03:04.513 [initandlisten] 
> 

我可以通过终端进入数据库,但现在在ADT中显示以下错误。

在使用32位ubuntu时有什么问题吗?

并且logcat是

04-09 10:03:47.175: W/com.mongodb.tcp(2682): Exception executing isMaster command on localhost/127.0.0.1:27017
04-09 10:03:47.175: W/com.mongodb.tcp(2682): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 2 7017) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at libcore.io.IoBridge.isConnected(IoBridge.java:223)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at libcore.io.IoBridge.connect(IoBridge.java:112)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at java.net.Socket.connect(Socket.java:843)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DBPort._open(DBPort.java:223)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DBPort.go(DBPort.java:125)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DBPort.go(DBPort.java:106)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DBPort.findOne(DBPort.java:162)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DBPort.runCommand(DBPort.java:170)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at   co m.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:547)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:526)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:236)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.mongodb.DB.getCollectionNames(DB.java:400)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.example.test1.InsertDocumentApp.executeHardWork(InsertDocumentApp.java:87)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.example.test1.InsertDocumentApp.access$1(InsertDocumentApp.java:76)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.example.test1.InsertDocumentApp$HugeWork.doInBackground(InsertDocumentApp.java:151)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at com.example.test1.InsertDocumentApp$HugeWork.doInBackground(InsertDocumentApp.java:1)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at java.lang.Thread.run(Thread.java:841)
04-09 10:03:47.175: W/com.mongodb.tcp(2682): Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    at libcore.io.IoBridge.isConnected(IoBridge.java:208)
04-09 10:03:47.175: W/com.mongodb.tcp(2682):    ... 27 more
04-09 10:03:47.175: W/com.mongodb(2682): emptying DBPortPool to localhost/127.0.0.1:27017 b/c of error
04-09 10:03:47.175: W/com.mongodb(2682): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 27017) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
04-09 10:03:47.175: W/com.mongodb(2682):    at libcore.io.IoBridge.isConnected(IoBridge.java:223)
04-09 10:03:47.175: W/com.mongodb(2682):    at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
04-09 10:03:47.175: W/com.mongodb(2682):    at libcore.io.IoBridge.connect(IoBridge.java:112)
04-09 10:03:47.175: W/com.mongodb(2682):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-09 10:03:47.175: W/com.mongodb(2682):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-09 10:03:47.175: W/com.mongodb(2682):    at java.net.Socket.connect(Socket.java:843)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.mongodb.DBPort._open(DBPort.java:223)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.mongodb.DBPort.go(DBPort.java:125)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.mongodb.DBPort.call(DBPort.java:92)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.mongodb.DB.getCollectionNames(DB.java:400)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.example.test1.InsertDocumentApp.executeHardWork(InsertDocumentApp.java:87)
 04-09 10:03:47.175: W/com.mongodb(2682):   at com.example.test1.InsertDocumentApp.access$1(InsertDocumentApp.java:76)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.example.test1.InsertDocumentApp$HugeWork.doInBackground(InsertDocumentApp.java:151)
04-09 10:03:47.175: W/com.mongodb(2682):    at com.example.test1.InsertDocumentApp$HugeWork.doInBackground(InsertDocumentApp.java:1)
04-09 10:03:47.175: W/com.mongodb(2682):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-09 10:03:47.175: W/com.mongodb(2682):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-09 10:03:47.175: W/com.mongodb(2682):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-09 10:03:47.175: W/com.mongodb(2682):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-09 10:03:47.175: W/com.mongodb(2682):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-09 10:03:47.175: W/com.mongodb(2682):    at java.lang.Thread.run(Thread.java:841)
04-09 10:03:47.175: W/com.mongodb(2682): Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
04-09 10:03:47.175: W/com.mongodb(2682):    at libcore.io.IoBridge.isConnected(IoBridge.java:208)
04-09 10:03:47.175: W/com.mongodb(2682):    ... 23 more
04-09 10:03:47.255: D/dalvikvm(2682): GC_FOR_ALLOC freed 215K, 1% free 117331K/117612K, paused 36ms, total 36ms
04-09 10:03:47.315: D/dalvikvm(2682): GC_FOR_ALLOC freed 27K, 1% free 119327K/119424K, paused 32ms, total 32ms
 04-09 10:03:47.375: D/dalvikvm(2682): GC_FOR_ALLOC freed 27K, 1% free 121338K/121432K, paused 34ms, total 34ms
04-09 10:03:47.375: I/System.out(2682): com.mongodb.MongoException$Network: Read operation to server localhost/127.0.0.1:27017 failed on database mydb

1 个答案:

答案 0 :(得分:1)

我不认为你可以在android里面使用MongoDb。请参阅此链接 MongoDB on Android

您所指的链接,即http://www.mkyong.com/mongodb/java-mongodb-insert-a-document/,是从不在Android内部的Java应用程序开始的。

我认为您的解决方案是使用sqlite或者couchbase,如果你想在你的android设备中安装一个数据库,或者你可以在服务器中安装一个MongoDb数据库(或使用像https://mongolab.com/这样的数据库)并调用这个MongoDb数据库来自Web服务器中安装的Web服务。所以设计看起来像这样。 你有自己的Android应用程序 - &gt;调用网络服务 - &gt;调用Mongodb