Соединение базы данных ConceptNet с Java
Кто-нибудь знает, как соединить ConceptNet Database с Java. Я искал разные учебники, проверял разные форумы, но все еще не мог найти правильную методологию.
Кроме того, как я могу получать и публиковать данные в / из ConceptNet, используя Java.
Некоторые люди говорят мне, что с помощью JSON или Flat Csv я получу ответ на свой запрос, но я не знаком с этими двумя технологиями или с тем, как их использовать с базами данных ConceptNet и Java.
Если кто-нибудь знает, пожалуйста, ответьте мне...
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();
}
}
}