2016-08-10 14 views
-2

私のプロジェクトをNeo4j DBに接続するためにtriyingを使用しています。私はGETリクエストをして、同じコード(POSTMethodのGETMethodを変更します)それはしない。私のPOSTリクエストの接続を行うことができません

これは私のコードです。

文字列名= "xxx"; 文字列パスワード= "xxx";

String authString = name + ":" + password; 

    byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); 

    String authStringEnc = new String(authEncBytes); 

    HttpMethod method = null; 
    HttpClient client = new HttpClient();  
    //String url = "http://localhost:7474/db/data/relationship"; 
    String url = "http://localhost:7474/db/data/cypher"; 


    method = new PostMethod(String.format(url)); 
    method.addRequestHeader(new Header("Content-Type", "application/json; charset=UTF-8")); 
    method.addRequestHeader("Authorization", "Basic "+ authStringEnc); 
    method.setQueryString("{\"query\" : \"MATCH (x{id:'123'})-[r]->(n) RETURN n.name, r.status\",\"params\" : { }}"); 

    try { 

     client.executeMethod(method); 

しかし、HttpExceptionがあります。

私はRestとNeo4jで初めてです。

誰かが私を助けることができますか?

ありがとうございました。

+1

_ "HttpExceptionがあります" _ - さて、例外とスタックトレースを私たちと共有しようとしていますか?あなたは、私たちが問題の原因を推測することを期待していますか?また、コード内のどのステートメントが例外をスローするかを教えてください。 –

+0

私はそれを解決できる、ありがとう。 – Silvia

答えて

0

回答がそこに行く:HTTP POST using JSON in Java

DefaultHttpClientを使用して試してみて、問題が解決しない場合もHttpPost代わりのHTTPMETHODを使用してみてください。

HttpClient client = new DefaultHttpClient();  
String url = "http://localhost:7474/db/data/cypher"; 

HttpPost method = new HttpPost(url); 

//You can use StringEntity for your query 
StringEntity params= new StringEntity("your query goes here", ContentType.APPLICATION_JSON); 
method.setEntity(params); 

//for the response use followings 
HttpResponse response = client.execute(method); 
HttpEntity entity = response.getEntity(); 
+0

それは動作します!大いに感謝する。 – Silvia

関連する問題