我在使用HTTPConnection运行cypher时遇到异常。
服务器返回HTTP响应代码:500为URL:http://localhost:7474/db/data/cypher
public class HTTPConnectionTest {
public static void main(String[] args) throws Exception {
try {
System.out.println("Testing HTTPConnection");
StringBuffer responseString = new StringBuffer();
String url = "http://localhost:7474/db/data/cypher";
//String query = "match (user:USER{id:\'Sree\'}) return user ";
String query = "match (user:USER{id:\"Sree\"}) return user ";
URL neo4jUrl = new URL(url);
HttpURLConnection httpConn = (HttpURLConnection) neo4jUrl
.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type", "application/json");
String urlParameters = "{\"query\":\"" + query + "\"}";
httpConn.setRequestProperty("Accept",
"application/json; charset=UTF-8");
DataOutputStream wr = new DataOutputStream(
httpConn.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
responseString.append(inputLine);
}
System.out.println("Out put " + responseString);
in.close();
} catch (Exception e) {
System.out.println("Exception" + e.getMessage());
}
}
}
如果我用单引号传递cypher中的值,它正在工作并获得输出对象。
String query =“match(user:USER {id:\'Sree \'})return user”;
有什么建议吗?提前谢谢!
答案 0 :(得分:1)
这对我来说很傻。
代码的细微变化。将params转换为JSONObject字符串
JSONObject jsonObject = new JSONObject();
jsonObject.put("query", query);
String urlParameters = jsonObject.toString();
而不是
String urlParameters = "{\"query\":\"" + query + "\"}";