添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I have an issue about connection from Android Studio to Neo4j database.
Please can you help?

The error is here:
org.neo4j.driver.exceptions.ServiceUnavailableException: Unable to connect to 127.0.0.1:7687, ensure the database is running and that there is a working network connection to it.

can you provide the log/error message, the code to create your driver, and the code queuing the database? Which driver are you using?

Maybe that will help.

I am using this driver (with gradle dependency):
implementation 'org.neo4j.driver:neo4j-java-driver:5.7.0'

Code in the MainActivity to create the driver (now where I copy the code I am asking myself if the driver should not be initialized when pressing the button (but before, on app start):

protected void onCreate(Bundle savedInstanceState) {
        Button countNodesBtn = findViewById(R.id.countNodesBtn);
        countNodesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Button clicked",Toast.LENGTH_LONG).show();
                countNodes();
private void countNodes() {
        Driver neoDriver = initDriver();
        try (var session = neoDriver.session()) {
            var query = "MATCH () RETURN count(*) AS count";
            var params = Values.parameters();
            var res = session.run(query, params).single().get("count").asLong();
            Toast.makeText(MainActivity.this, "Found " + res + " nodes", Toast.LENGTH_LONG).show();
static Driver initDriver() {
        // TODO: Create and assign an instance of the driver here
        AuthToken auth = AuthTokens.basic("neo4j", "myPassword");
        Driver driver = GraphDatabase.driver("bolt://localhost:8080", auth);
        driver.verifyConnectivity();
        return driver;

I checked the "enable dev mode in neo4j desktop and entered the 8080 port there (but it did not work before neither)

the result of the query in neo4j desktop delivers 8 as count

and here the error/log:

2023-05-18 08:27:32.638 13202-13202 AndroidRuntime          com.example.beneficialfoods          E  FATAL EXCEPTION: main
                                                                                                    Process: com.example.beneficialfoods, PID: 13202
                                                                                                    org.neo4j.driver.exceptions.ServiceUnavailableException: Unable to connect to localhost:8080, ensure the database is running and that there is a working network connection to it.
                                                                                                    	at org.neo4j.driver.internal.util.ErrorUtil.rethrowAsyncException(ErrorUtil.java:113)
                                                                                                    	at org.neo4j.driver.internal.util.Futures.blockingGet(Futures.java:111)
                                                                                                    	at org.neo4j.driver.internal.util.Futures.blockingGet(Futures.java:89)
                                                                                                    	at org.neo4j.driver.internal.InternalDriver.verifyConnectivity(InternalDriver.java:159)
                                                                                                    	at com.example.beneficialfoods.MainActivity.initDriver(MainActivity.java:125)
                                                                                                    	at com.example.beneficialfoods.MainActivity.countNodes(MainActivity.java:107)
                                                                                                    	at com.example.beneficialfoods.MainActivity.-$$Nest$mcountNodes(Unknown Source:0)
                                                                                                    	at com.example.beneficialfoods.MainActivity$2.onClick(MainActivity.java:77)
                                                                                                    	at android.view.View.performClick(View.java:7506)
                                                                                                    	at android.view.View.performClickInternal(View.java:7483)
                                                                                                    	at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
                                                                                                    	at android.view.View$PerformClick.run(View.java:29334)
                                                                                                    	at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                    	at android.os.Looper.loop(Looper.java:288)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:7872)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
                                                                                                    	Suppressed: org.neo4j.driver.internal.util.ErrorUtil$InternalExceptionCause
                                                                                                    		at org.neo4j.driver.internal.async.connection.ChannelConnectedListener.databaseUnavailableError(ChannelConnectedListener.java:72)
                                                                                                    		at org.neo4j.driver.internal.async.connection.ChannelConnectedListener.operationComplete(ChannelConnectedListener.java:66)
                                                                                                    		at org.neo4j.driver.internal.async.connection.ChannelConnectedListener.operationComplete(ChannelConnectedListener.java:36)
                                                                                                    		at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:590)
                                                                                                    		at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:583)
                                                                                                    		at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:559)
                                                                                                    		at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:492)
                                                                                                    		at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:636)
                                                                                                    		at io.netty.util.concurrent.DefaultPromise.setFailure0(DefaultPromise.java:629)
                                                                                                    		at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:118)
                                                                                                    		at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:321)
                                                                                                    		at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:337)
                                                                                                    		at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776)
                                                                                                    		at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
                                                                                                    		at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
                                                                                                    		at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
                                                                                                    		at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
                                                                                                    		at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
                                                                                                    		at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
                                                                                                    		at java.lang.Thread.run(Thread.java:1012)
                                                                                                    Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:8080
                                                                                                    Caused by: java.net.ConnectException: Connection refused
                                                                                                    	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
                                                                                                    	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:762)
                                                                                                    	at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337)
                                                                                                    	at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
2023-05-18 08:27:32.638 13202-13202 AndroidRuntime          com.example.beneficialfoods          E  	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776)
                                                                                                    	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
                                                                                                    	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
                                                                                                    	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
                                                                                                    	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
                                                                                                    	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
                                                                                                    	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
                                                                                                    	at java.lang.Thread.run(Thread.java:1012)
Screenshot 2023-05-18 at 08.21.101636×1284 236 KB
              

In your initial code you created your driver with bolt protocol pointing port 8080. The bolt protocol is on 7687.

You should create one driver at application start up and reuse throughout your application. Get a new session from the driver each time you want to interact with the database.