2015-02-06 840 views
1

我在使用HTTPConnection運行cypher時遇到異常。Neo4j - 服務器返回的HTTP響應代碼:500的URL:http:// localhost:7474/db/data/cypher

服務器返回的HTTP響應代碼:500網址: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()); 
      } 
    } 
} 

如果我用單引號這是工作和獲取輸出對象暗號傳遞價值。

String query =「match(user:USER {id:\'Sree \'})return user」;

有什麼建議嗎?先謝謝你!

回答

1

這看起來很愚蠢。

代碼中的小改動。轉換成PARAMS字符串的JSONObject

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("query", query); 
String urlParameters = jsonObject.toString(); 

,而不是

String urlParameters = "{\"query\":\"" + query + "\"}"; 
相關問題