我想在我的Android项目中使用neo4j DB。我尝试使用Android连接Neo4j数据库,同时连接我收到错误。我在Android应用级别gradle中添加了依赖项。
compile 'org.neo4j.driver:neo4j-java-driver:1.3.0'
MainActivity.java
Driver driver = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic("neo4j", "neo4j") );
Session session = driver.session();
session.run( "CREATE (a:Person {name: {name}, title: {title}})",
parameters( "name", "Arthur", "title", "King" ) );
StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = {name} " +
"RETURN a.name AS name, a.title AS title",
parameters( "name", "Arthur" ) );
while ( result.hasNext() )
{
Record record = result.next();
System.out.println( record.get( "title" ).asString() + " " + record.get( "name" ).asString() );
}
session.close();
driver.close();
日志
06-22 16:24:05.627 4990-4990/com.example.prateek.neo4jtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.prateek.neo4jtest, PID: 4990
java.lang.NoClassDefFoundError: java.net.StandardSocketOptions
at org.neo4j.driver.internal.net.ChannelFactory.create(ChannelFactory.java:39)
at org.neo4j.driver.internal.net.SocketClient.start(SocketClient.java:124)
at org.neo4j.driver.internal.net.SocketConnection.startSocketClient(SocketConnection.java:92)
at org.neo4j.driver.internal.net.SocketConnection.<init>(SocketConnection.java:67)
at org.neo4j.driver.internal.net.SocketConnector.createConnection(SocketConnector.java:77)
at org.neo4j.driver.internal.net.SocketConnector.connect(SocketConnector.java:50)
at org.neo4j.driver.internal.net.pooling.SocketConnectionPool$ConnectionSupplier.get(SocketConnectionPool.java:204)
at org.neo4j.driver.internal.net.pooling.SocketConnectionPool$ConnectionSupplier.get(SocketConnectionPool.java:186)
at org.neo4j.driver.internal.net.pooling.BlockingPooledConnectionQueue.acquire(BlockingPooledConnectionQueue.java:96)
at org.neo4j.driver.internal.net.pooling.SocketConnectionPool.acquireConnection(SocketConnectionPool.java:137)
at org.neo4j.driver.internal.net.pooling.SocketConnectionPool.acquire(SocketConnectionPool.java:76)
at org.neo4j.driver.internal.DirectConnectionProvider.acquireConnection(DirectConnectionProvider.java:45)
at org.neo4j.driver.internal.NetworkSession.acquireConnection(NetworkSession.java:340)
at org.neo4j.driver.internal.NetworkSession.run(NetworkSession.java:104)
at org.neo4j.driver.internal.NetworkSession.run(NetworkSession.java:94)
at com.example.prateek.neo4jtest.MainActivity$1.onClick(MainActivity.java:29)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18431)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
任何帮助将不胜感激。 谢谢!
答案 0 :(得分:1)
仅仅因为有一个Java驱动程序,并不意味着它适用于Android。
(虽然看起来Github上有一个过时的Android驱动程序)
Neo4j exposes a REST API你可以使用给定的HTTP请求,这可能是最安全的使用方法,尽管它需要学习如何在Android上运行并处理JSON解析。
更具体地说,错误是StandardSocketOptions
在API 24之前不属于Android的一部分,因此您可以尝试将您的compileSdk和minimumSdk设置为24,但不能保证它仍然有效。
顺便说一句,除非您在Android设备上运行Neo4j服务器,否则无法从Android设备连接到localhost
答案 1 :(得分:0)
java.net.StandardSocketOptions已添加到API级别26的Android平台中。
因此,如果您的应用可以接受目标API级别26或更高级别,那么您至少可以通过该异常。但是,您可能仍会看到驱动程序所需的其他类的类似问题。