0

i am trying to connect to mogoDB database, i installed mongoDB as explained in

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

and played with mongo via mongoshell in ubuntu terminal, everything is savvy, then i tried to implement it in an android application. i referred to this tutorial

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

and edited it to create my code listed below

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);
    }
}

}

i noticed that some imports in the tutorial are not coming in ADT even after i imported jar file which i got from this link

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

this is the jar file i downloaded mongo-java-driver-2.11.3.jar i imported it by, project/buildpath/add external jar errors are showing everywhere with 'mongo items' are there. is it because eclipse is not recognizing jar file i added to build path? is this the correct jar file? do i need to do anything more to get my app running?

this is the terminal prompt which occurs as i type mongo in terminal

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] 
> 

i can get into database via terminal, but in ADT now the following errors are shown.

is der any problem in using 32bit ubuntu?

and the logcat is

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
Mukund
  • 1,107
  • 2
  • 18
  • 40

1 Answers1

1

I dont think that you can use MongoDb from inside android. Refer to this link MongoDB on Android

The link that you are referring, namely the http://www.mkyong.com/mongodb/java-mongodb-insert-a-document/ is to work from a java app not from inside android.

I think your solution is to either use sqlite or couchbase if you want a database inside your android device or maybe you can install a MongoDb database in a server (or use one like https://mongolab.com/ ) and call this MongoDb database from a web service that is installed in a web server. So the design could look like this. You have your android app-->calls the web service--> calls the Mongodb

Community
  • 1
  • 1
Periklis Douvitsas
  • 2,431
  • 1
  • 13
  • 14
  • ok, but, i want to know what is the reason of the errors occuring in my application? if i import jar file atleast the app should compile ri8? here errors are occuring even before the app is run, so i guz that the java-mongo-driver which i downloaded does not support android? – Mukund Apr 08 '14 at 07:35
  • 1
    Well i think you are right, that the MongoDb driver now supports android but i dont know still if this is a good idea. Look at these links. http://stackoverflow.com/questions/17913641/connecting-to-a-mongodb-with-android-in-eclipse http://stackoverflow.com/questions/11433091/how-connect-mongo-db-with-android-application – Periklis Douvitsas Apr 08 '14 at 07:54
  • 1
    When you put the MongoDb import like import com.mongodb.DBCollection; do you see errors in these imports? – Periklis Douvitsas Apr 08 '14 at 07:55
  • i didnt do that, lemme check – Mukund Apr 08 '14 at 08:46
  • i did and all the errors are gone now its compiling and running, but exception is occuring `04-09 05:27:59.671: W/System.err(1236): Caused by: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 27017) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)` – Mukund Apr 09 '14 at 05:38
  • Have you started the MongoDb server in your localhost? You can start MongoDB from a command line by issuing the mongod command. http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/ – Periklis Douvitsas Apr 09 '14 at 06:55
  • from the terminal, if you type mongo localhost:27017 can you connect also? – Periklis Douvitsas Apr 09 '14 at 12:38
  • they say that journaling defaults to off for 32 bits and is currently off – Mukund Apr 11 '14 at 06:28
  • do u know what is journaling? if it is off? can we connect database via android? – Mukund Apr 11 '14 at 11:03