【问题标题】:ConceptNet Database Connectivity with Java使用 Java 的 ConceptNet 数据库连接
【发布时间】:2016-07-10 18:38:07
【问题描述】:
有人知道如何将 ConceptNet 数据库与 Java 连接起来吗?我搜索了不同的教程,查看了不同的论坛,但仍然找不到正确的方法。
另外,我如何使用 Java 从 ConceptNet 获取和发布数据。
有人告诉我,通过使用 JSON 或 Flat Csv,我将实现查询的回复,但我不熟悉这两种技术,也不熟悉如何将它们与 ConceptNet 数据库和 Java 一起使用。
如果有人知道,请回复我...
【问题讨论】:
标签:
java
json
csv
conceptnet
【解决方案1】:
这就是我从 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();
}
}
}