如何使用 android 中的 asynctask 连接到 neo4j 服务器(本地主机)
How can I use asynctask from android to connect to neo4j server(localhost)
我在 mac 上安装了带有 localhost:7474 的 neo4j。如何通过来自 android 的异步任务添加新的人名和 ID?请帮助 me.I 我已经尝试了 2 周,但仍然没有结果。
我添加了一些我试过的代码,但我猜嵌入式数据库在 android 中不起作用:
也尝试过:
RestAPI graphDb = new RestAPIFacade("http://localhost:7474/db/data");
QueryEngine engine=new RestCypherQueryEngine(graphDb);
QueryResult<Map<String,Object>> result = engine.query("create (:Person {name:\"Steve\"}) return n", Collections.EMPTY_MAP);
Iterator<Map<String, Object>> iterator=result.iterator();
if(iterator.hasNext()) {
Map<String,Object> row= iterator.next();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(getApplicationContext(),"" + row.get("total") , duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
它还在崩溃。
错误:
java.lang.NoClassDefFoundError:解析失败:Lorg/neo4j/rest/graphdb/RestAPIFacade;
还有:
原因:java.lang.ClassNotFoundException:在路径上找不到 class "org.neo4j.rest.graphdb.RestAPIFacade":DexPathList[[zip 文件“/data/app/com.example.1/base.apk”], nativeLibraryDirectories=[/vendor/lib, /system/lib]]
/////////
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JSONAsyncTask().execute();
}
class JSONAsyncTask extends AsyncTask<Void, Void, JSONArray> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONArray doInBackground(Void... urls) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:7474/db/data");
// // Add your data
// List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
// nameValuePairs.add(new BasicNameValuePair("node","1"));
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
reader.close();
String result = sb.toString();
Log.d("log",result);
// parsing data
return new JSONArray(result);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(JSONArray result) {
}
}
////////////
{"extensions":{},"outgoing_relationships":"http://ip_add:7474/db/data/node/6/relationships/out","labels":"http://ip_add:7474/db/data/node/6/labels","traverse":"http://ip_add:7474/db/data/node/6/traverse/{returnType}","all_typed_relationships":"http://ip_add:7474/db/data/node/6/relationships/all/{-list|&|types}","self":"http://ip_add:7474/db/data/node/6","property":"http://ip_add:7474/db/data/node/6/properties/{key}","properties":"http://ip_add:7474/db/data/node/6/properties","outgoing_typed_relationships":"http://ip_add:7474/db/data/node/6/relationships/out/{-list|&|types}","incoming_relationships":"http://ip_add:7474/db/data/node/6/relationships/in","create_relationship":"http://ip_add:7474/db/data/node/6/relationships","paged_traverse":"http://ip_add:7474/db/data/node/6/paged/traverse/{returnType}{?pageSize,leaseTime}","all_relationships":"http://ip_add:7474/db/data/node/6/relationships/all","incoming_typed_relationships":"http://ip_add:7474/db/data/node/6/relationships/in/{-list|&|types}","metadata":{"id":6,"labels":[]},"data":{}}
您需要将必要的 JAR 文件添加到您的 android 项目中。
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ neo4j-rest-graphdb ---
[INFO] org.neo4j:neo4j-rest-graphdb:jar:1.9.3-SNAPSHOT
[INFO] +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.7:compile
[INFO] | \- org.codehaus.jackson:jackson-core-asl:jar:1.9.7:compile
[INFO] +- org.neo4j:neo4j-kernel:jar:1.9.2:compile
[INFO] | \- org.apache.geronimo.specs:geronimo-jta_1.1_spec:jar:1.1.1:compile
[INFO] +- org.neo4j:neo4j-lucene-index:jar:1.9.2:compile
[INFO] | \- org.apache.lucene:lucene-core:jar:3.6.2:compile
[INFO] | +- com.sun.jersey:jersey-core:jar:1.4:compile
[INFO] | \- asm:asm:jar:3.1:compile
[INFO] \- com.sun.jersey:jersey-client:jar:1.4:compile
但也许先从一个简单的 http 请求开始,然后尝试更多。
我在 mac 上安装了带有 localhost:7474 的 neo4j。如何通过来自 android 的异步任务添加新的人名和 ID?请帮助 me.I 我已经尝试了 2 周,但仍然没有结果。
我添加了一些我试过的代码,但我猜嵌入式数据库在 android 中不起作用:
也尝试过:
RestAPI graphDb = new RestAPIFacade("http://localhost:7474/db/data");
QueryEngine engine=new RestCypherQueryEngine(graphDb);
QueryResult<Map<String,Object>> result = engine.query("create (:Person {name:\"Steve\"}) return n", Collections.EMPTY_MAP);
Iterator<Map<String, Object>> iterator=result.iterator();
if(iterator.hasNext()) {
Map<String,Object> row= iterator.next();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(getApplicationContext(),"" + row.get("total") , duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
它还在崩溃。
错误: java.lang.NoClassDefFoundError:解析失败:Lorg/neo4j/rest/graphdb/RestAPIFacade;
还有: 原因:java.lang.ClassNotFoundException:在路径上找不到 class "org.neo4j.rest.graphdb.RestAPIFacade":DexPathList[[zip 文件“/data/app/com.example.1/base.apk”], nativeLibraryDirectories=[/vendor/lib, /system/lib]]
/////////
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JSONAsyncTask().execute();
}
class JSONAsyncTask extends AsyncTask<Void, Void, JSONArray> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONArray doInBackground(Void... urls) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:7474/db/data");
// // Add your data
// List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
// nameValuePairs.add(new BasicNameValuePair("node","1"));
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
reader.close();
String result = sb.toString();
Log.d("log",result);
// parsing data
return new JSONArray(result);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(JSONArray result) {
}
}
////////////
{"extensions":{},"outgoing_relationships":"http://ip_add:7474/db/data/node/6/relationships/out","labels":"http://ip_add:7474/db/data/node/6/labels","traverse":"http://ip_add:7474/db/data/node/6/traverse/{returnType}","all_typed_relationships":"http://ip_add:7474/db/data/node/6/relationships/all/{-list|&|types}","self":"http://ip_add:7474/db/data/node/6","property":"http://ip_add:7474/db/data/node/6/properties/{key}","properties":"http://ip_add:7474/db/data/node/6/properties","outgoing_typed_relationships":"http://ip_add:7474/db/data/node/6/relationships/out/{-list|&|types}","incoming_relationships":"http://ip_add:7474/db/data/node/6/relationships/in","create_relationship":"http://ip_add:7474/db/data/node/6/relationships","paged_traverse":"http://ip_add:7474/db/data/node/6/paged/traverse/{returnType}{?pageSize,leaseTime}","all_relationships":"http://ip_add:7474/db/data/node/6/relationships/all","incoming_typed_relationships":"http://ip_add:7474/db/data/node/6/relationships/in/{-list|&|types}","metadata":{"id":6,"labels":[]},"data":{}}
您需要将必要的 JAR 文件添加到您的 android 项目中。
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ neo4j-rest-graphdb ---
[INFO] org.neo4j:neo4j-rest-graphdb:jar:1.9.3-SNAPSHOT
[INFO] +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.7:compile
[INFO] | \- org.codehaus.jackson:jackson-core-asl:jar:1.9.7:compile
[INFO] +- org.neo4j:neo4j-kernel:jar:1.9.2:compile
[INFO] | \- org.apache.geronimo.specs:geronimo-jta_1.1_spec:jar:1.1.1:compile
[INFO] +- org.neo4j:neo4j-lucene-index:jar:1.9.2:compile
[INFO] | \- org.apache.lucene:lucene-core:jar:3.6.2:compile
[INFO] | +- com.sun.jersey:jersey-core:jar:1.4:compile
[INFO] | \- asm:asm:jar:3.1:compile
[INFO] \- com.sun.jersey:jersey-client:jar:1.4:compile
但也许先从一个简单的 http 请求开始,然后尝试更多。