-4
위키 백과 URL을 자바 언어로 빌드하려면 어떻게해야합니까? 예를 들어 "javascript"에 대한 검색의 경우 URL은 다음과 같습니다. https://en.wikipedia.org/wiki/JavaScript위키 백과 URL 작성
사용할 수있는 API가 있습니까?
위키 백과 URL을 자바 언어로 빌드하려면 어떻게해야합니까? 예를 들어 "javascript"에 대한 검색의 경우 URL은 다음과 같습니다. https://en.wikipedia.org/wiki/JavaScript위키 백과 URL 작성
사용할 수있는 API가 있습니까?
나는 그런 API를 알지 못합니다. 그러나 당신은 당신의 자신의 공장 방법을 만들 수 있습니다
public class WikipediaURLFactory {
private static final String WIKIPEDIA_BASE_URL = "https://en.wikipedia.org/wiki/";
public static String createWikiURLString(String search) {
return WIKIPEDIA_BASE_URL + search;
}
public static URL createWikiURL(String search) throws MalformedURLException {
return new URL(createWikiURLString(search));
}
public static Status accessPage (URL url) throws IOException {
Status status = new Status();
status.setUrl(url);
status.setExists(true);
if (getResponseCode(url) == 404) {
status.setExists(false);
}
return status;
}
private static int getResponseCode (URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
return connection.getResponseCode();
}
}
귀하의 상태 클래스 :
public class Main {
public static void main(String[] args) {
try {
// this will return true
URL url = WikipediaURLFactory.createWikiURL("JavaScript");
Status status = WikipediaURLFactory.accessPage(url);
String negation = status.isExists() ? "" : "doesn't";
System.out.println("The webpage " + url + " " + negation + " exist");
// this will return false as page JafaScript doesn't exist on wiki
url = WikipediaURLFactory.createWikiURL("JafaScript");
status = WikipediaURLFactory.accessPage(url);
negation = status.isExists() ? "" : "doesn't";
System.out.println("The webpage " + url + " " + negation + " exist");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
당신은 상태에서 다른 필요한 필드를 추가 할 수 있습니다 : 여기
private boolean exists;
private URL url;
public Status() {}
public boolean isExists() {
return exists;
}
public void setExists(boolean exists) {
this.exists = exists;
}
public URL getUrl() {
return url;
}
public void setUrl(URL url) {
this.url = url;
}
그리고 메인 테스트 클래스이다 필요한 경우 클래스 (예 : 페이지 콘텐츠)를 이것은 단지 예일뿐입니다.
우리가 찾고있는 정보가 wikidata 데이터베이스에 존재하는지 아닌지를 확인하기 위해 예를 들어 좀 더 개선 된 것을 사용하고 싶습니다. –
내 대답이 업데이트되어 페이지에 연결할 수없는 경우 부울을 반환합니다. 거기에서 자신 만의 것을 만들 수 있습니다. 공개적으로 사용 가능한 API에 대해 알지 못합니다. – user3362334