gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:10:16 26 4
gpt4 key购买 nike

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

中的说明安装了 mongoDB

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

并通过 mongoshell 在 ubuntu 终端中使用 mongo,一切都很明智,然后我尝试在 Android 应用程序中实现它。我引用了这个教程

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

并对其进行编辑以创建下面列出的代码

包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我通过项目/构建路径/添加外部 jar 导入它错误随处可见,其中有“mongo items”。是因为 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

最佳答案

我不认为你可以从 android 内部使用 MongoDb。引用这个链接 MongoDB on Android

您引用的链接,即 http://www.mkyong.com/mongodb/java-mongodb-insert-a-document/是从java应用程序而不是从android内部工作。

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

关于java - 连接mongoDB的代码错误,导入的jar无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22930171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com