[英]ConceptNet Database Connectivity with Java
有人知道如何將ConceptNet數據庫與Java連接。 我搜索了不同的教程,檢查了不同的論壇,但我仍然找不到正確的方法。
此外,如何使用Java從/從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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.