We are currently designing a user registration interface in Android Studio. The data for the users are non-relational. Thus, we chose to use Neo4j. We designed to connect the Neo4j with the studio in Java. It worked in Eclipse and successfully added data to the database. However, in Android Studio it does not work, and gives an error message like NetworkOnMainThreadException
, even after we permitted use of http in the manifest.xml
file. Also, we experimented with different kinds of possible localhost addresses and none of them worked. Our search on Stack Overflow revealed that adding a new thread might provide a solution. If so, how could we do that?
We also looked at that question but it is not the same situation here. We can connect it to the eclipse but not android and it is about neo4j database connection with the android.
public User(String username, int age) {
driver = GraphDatabase.driver("bolt://10.0.2.2:7687");
this.username = username;
this.age = age;
createUser();
}
// after one user registers, add it to DB(get its username(unique) & age):
private void createUser() {
String query = "create (u:User {name: \"" + username + "\", age: " + age + "})";
try (Session session = driver.session()) {
session.run(query);
}
}
We want it to connect Neo4j and put user registration information as they are entered in the Android Emulator for now.
android.os.NetworkOnMainThreadException at
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1450)