如何解决 neo4j 错误(参数必须是 JSON 映射)时尝试在 Java 中进行 REST API 密码查询



我目前正在尝试使用我的neo4j数据库执行Cypher查询,作为我开始使用和练习使用REST API的Android Studio应用程序的一部分。其代码如下所示:

public class MainActivity extends AppCompatActivity {
private TextView userText;
private Button clicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userText = (TextView) findViewById(R.id.txtUsername);
clicker = (Button) findViewById(R.id.btnClick);
clicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//The REST API needs it's own thread for Neo4j connection. Read up on this documentation.
AsyncTask.execute(new Runnable() {
@Override
public void run() {
try {
//The URL will change based on what is needed for the documentations.
//Provides the address for the HTTP communication.
String neo4jURL = "http://ec2-35-176-79-15.eu-west-2.compute.amazonaws.com:7474/db/data/cypher";
//Helps to create the JSON object used by the query.
//QueryData neo4jqueryData = new QueryData();
//Creates the client used for the connection.
HttpClient neo4jClient = new DefaultHttpClient();
//Assigns the address to either the HttpGet or HttpPost request.
HttpPost neo4jRequest = new HttpPost(neo4jURL);
//Creates the JSON Attributes that will be used as part of the query.
String query = "MATCH (n:Student) WHERE n.Username='cs16thm' RETURN n.StudentID";
JSONObject neo4jJSON = new JSONObject();
neo4jJSON.put("query",query);
neo4jJSON.put("params","");;
String check = neo4jJSON.toString();
Log.d("JSON",check);
StringEntity queryString = new StringEntity(check,"UTF-8");
//Ensures that the application type is JSON.
queryString.setContentType("application/json");
//Adds the Attribute data to the JSON query.
neo4jRequest.setEntity(queryString);
//Adds the Headers to the query.
//bmVvNGo6Z3JvdXAzNw== is the Base64 encoding of the username and password.
neo4jRequest.setHeader("Authorization","Basic bmVvNGo6Z3JvdXAzNw==");
//Shows the data types that would be accepted by the query result.
neo4jRequest.setHeader("Accept", "application/json; charset=UTF-8");
//Used to show the data type being sent to the server.
neo4jRequest.setHeader("Content-Type", "application/json");
Log.d("Check-Type",neo4jRequest.getEntity().toString());
//Performs the neo4j query.
HttpResponse queryResult = neo4jClient.execute(neo4jRequest);
//Checks the status code of the request.
int statusCode = queryResult.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 400){
Log.d("CONNECT","Query Made");
//Gets the results stream from the connection. Results is returned as a JSON which needs to be parsed.
InputStreamReader resultsReader = new InputStreamReader(queryResult.getEntity().getContent());
JsonReader JsonReader = new JsonReader(resultsReader);
//Begins processing of the JSON results.
JsonReader.beginObject();
//Checks all of the parameter keys returned by the document.
Log.d("RESULT","Begin JSON Parse");
while(JsonReader.hasNext()){
//Gathers the name of the current key.
String resultKey = JsonReader.nextName();
//Checks if it's the data we're looking for.
if(resultKey.equals("message")){
//Gathers the String value that we require.
String result = JsonReader.nextString();
//Shows the result in the application.
Log.d("RESULT","Result=" + result);
//Prevents further loop execution now that our data is retrieved.
break;
} else {
//Skips to the next value if the current one contains nothing of interest.
JsonReader.skipValue();
}
}
Log.d("RESULT","End JSON Parse");
//Closes the JSON reader after task to prevent resource hogging.
JsonReader.close();
} else {
Log.d("ERROR", "Request not made " + Integer.toString(statusCode));
}

} catch (MalformedURLException e) {
e.printStackTrace();
Log.d("ERROR", "Bad URL");
} catch (IOException ioe) {
ioe.printStackTrace();
Log.d("ERROR", "Cannot Connect");
} catch (Exception par){
par.printStackTrace();
Log.d("ERROR", "Parse Fail");
}
}
});
}
});
}
}

虽然我的程序确实成功接收了来自 neo4j Amazon EC2 服务器的响应,但它回复了 HTTP 400 错误,并提供以下错误消息,这些错误消息存在于输出 JSON 字符串中:Result=Parameters must be a JSON mapResult=IllegalArgumentException Result=java.lang.IllegalArgumentException

由于这些消息,我试图确保我的查询实体采用有效的 JSON 格式,但尽管如此,错误消息似乎仍然出现。尽管在互联网资源中进行了大量搜索,但我无法找到解决此问题的方法。我目前正在使用 Apache HttpClient 和 Google 的 Simple JSON 外部库。

如果有人能提供任何见解或解决方案来帮助我解决这个问题,我将不胜感激。我也有兴趣看到任何其他可以建立我的REST API连接的Java代码。但是,我可以确认Neo4j提供的许多官方驱动程序,例如Neo4j-Java-Driver在尝试通过Bolt协议连接时似乎与Android Studio不兼容。最后,如果您有任何其他问题要问我,请随时提问。

更新:
省略 cybersam 建议的"参数"值确实可以解决错误并为成功查询生成 HTTP 200 代码。但是,当我尝试通过将行 if(resultKey.equals("message")) 更改为 if(resultKey.equals("data")) 来读取 Neo4j 提供的 JSON 文件作为查询结果时,我收到以下错误消息:java.lang.IllegalStateException:预期一个字符串,但BEGIN_ARRAY在字符串结果 = JsonReader.nextString();以及一个 ParseException。Neo4j文档说,JSON文件应该采用类似于这样的格式:{ "columns" : [ "n.StudentID" ], "data" : ["1607174"] }

您当前正在将""设置为params的值。由于""不是 JSON 映射,因此您会收到该错误。

您可以将{}设置为params的值,或者更好的是,省略params字段的整个put语句,因为该字段是可选的。

最新更新