Thursday, August 8, 2013

Calling Neo4j query Using Rest Interface Sample code

The following code can be used to query Neo4j using REST interface.
I have used HttpClient 4.2 . Please download and include it in the project.

static String readURL(){
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://scim-dev:7474/db/data/cypher");
try {
post.setHeader("Accept", "application/json");
post.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(
"{\n  \"query\" : \"Your Query Here",\n  \"params\" : {\n  }\n}");
post.setEntity(entity);

HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
StringBuffer buff= new StringBuffer(line);
while ((line = rd.readLine()) != null) {
buff.append(line);
//System.out.println(line);
}
return buff.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

No comments: