有人知道如何用 Java 连接 ConceptNet 数据库吗?我搜索了不同的教程,检查了不同的论坛,但我仍然找不到正确的方法。
此外,如何使用 Java 向 ConceptNet 获取数据或从 ConceptNet 发布数据。
有人告诉我,通过使用 JSON 或 Flat Csv,我可以实现查询的回复,但我不熟悉这两种技术,也不熟悉如何将它们与 ConceptNet 数据库和 Java 一起使用。
如果有人知道请回复我...
最佳答案
这是我从 ConceptNet 访问查询所做的操作。我使用了 org.apache.commons.io.IOUtils 和 org.json maven 目录。希望这会有所帮助。
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import org.apache.commons.io.IOUtils;
import org.json.*;
public class httprequestpractice {
public static void main(String[] args) {
try {
// url containing the word to be indexed
String obj = "http://api.conceptnet.io/c/en/example";
// open HttpURLConnection
HttpURLConnection hp = (HttpURLConnection) new URL(obj)
.openConnection();
// set to request method to get
// not required since default
hp.setRequestMethod("GET");
// get the inputstream in the json format
hp.setRequestProperty("Accept", "application/json");
// get inputstream from httpurlconnection
InputStream is = hp.getInputStream();
// get text from inputstream using IOUtils
String jsonText = IOUtils.toString(is, Charset.forName("UTF-8"));
// get json object from the json String
JSONObject json = new JSONObject(jsonText);
// get the edges array from the JSONObject which contains all
// content
JSONArray edges = json.getJSONArray("edges");
// goes through the edges array
for (int x = 0; x < edges.length(); x++) {
// convert the first object of the json array into a jsonobject
// once it is a jsonobject you can use getString or getJSONArray
// to continue in getting info
System.out.println(
edges.getJSONObject(x));
}
is.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
关于java - ConceptNet 数据库与 Java 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295253/