Erinevus lehekülje "ITI0011:praktikum 14" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
(Ago teisaldas lehekülje ITI0011:praktikum 14 pealkirja ITI0011-2014:praktikum 14 alla)
 
 
1. rida: 1. rida:
#suuna [[ITI0011-2014:praktikum 14]]
+
== Kood ==
 +
 
 +
<source lang="java">
 +
package wiki;
 +
 
 +
import java.io.BufferedReader;
 +
import java.io.IOException;
 +
import java.io.InputStreamReader;
 +
import java.net.MalformedURLException;
 +
import java.net.URL;
 +
import java.net.URLEncoder;
 +
 
 +
/**
 +
* Wikipedia API which allows to
 +
* extract information about an entity.
 +
* A simple example of jar creation
 +
* and usage.
 +
* @author Ago
 +
*
 +
*/
 +
public class WikiAPI {
 +
 
 +
 +
/**
 +
* Used to get output in json format.
 +
*/
 +
public static int FORMAT_JSON = 1;
 +
/**
 +
* Used to get output in xml format.
 +
*/
 +
public static int FORMAT_XML = 2;
 +
 +
public static void main(String[] args) {
 +
// TODO Auto-generated method stub
 +
System.out.println(getWikiInformation("Main page", WikiAPI.FORMAT_JSON));
 +
System.out.println(getWikiInformation("Estonia", WikiAPI.FORMAT_JSON));
 +
System.out.println(getWikiInformation("Tallinn", WikiAPI.FORMAT_JSON, "info", "recentchanges"));
 +
 
 +
}
 +
 +
 +
/**
 +
* Uses Wikipedia API to retrieve general information about
 +
* a certain page in XML-format.
 +
* @param pageName Page title to be searched for
 +
* @return Response in XML-format
 +
*/
 +
public static String getWikiInformation(String pageName) {
 +
return getWikiInformation(pageName, FORMAT_XML);
 +
}
 +
 +
/**
 +
* Uses Wikipedia API to retrieve general information about
 +
* a certain page.
 +
* @param pageName Page title to be searched for
 +
* @param format In which format the response is returned,
 +
* use WikiAPI.FORMAT_* constants.
 +
* @return Response in the specified format
 +
*/
 +
public static String getWikiInformation(String pageName, int format) {
 +
return getWikiInformation(pageName, format, "info", null);
 +
}
 +
 +
/**
 +
* Uses Wikipedia API to retrieve information about
 +
* a certain page.
 +
* @param pageName Page title to be searched for
 +
* @param format In which format the response is returned,
 +
* use WikiAPI.FORMAT_* constants.
 +
* @param prop Value for "prop" parameter (properties)
 +
* @param list Value for "list" parameter
 +
* @return Response in the specified format
 +
*/
 +
public static String getWikiInformation(String pageName, int format,
 +
String prop, String list) {
 +
 +
String formatString = "json";
 +
if (format == WikiAPI.FORMAT_XML) {
 +
formatString = "xml";
 +
}
 +
String listStr = "";
 +
if (list != null) {
 +
listStr = "&list=" + list;
 +
}
 +
try {
 +
URL url = new URL("http://en.wikipedia.org/w/api.php?format="
 +
+ formatString
 +
+ "&continue="
 +
+ "&action=query&titles="
 +
+ URLEncoder.encode(pageName, "UTF-8")
 +
+ "&prop=" + prop + listStr);
 +
BufferedReader br = new BufferedReader(
 +
new InputStreamReader(url.openStream()));
 +
String line;
 +
StringBuilder sb = new StringBuilder();
 +
while ((line = br.readLine()) != null) {
 +
sb.append(line);
 +
}
 +
return sb.toString();
 +
 +
} catch (MalformedURLException e) {
 +
// TODO Auto-generated catch block
 +
return null;
 +
} catch (IOException e) {
 +
// TODO Auto-generated catch block
 +
return null;
 +
}
 +
}
 +
 
 +
}
 +
 
 +
</source>

Viimane redaktsioon: 19. märts 2015, kell 08:58

Kood

<source lang="java"> package wiki;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder;

/**

* Wikipedia API which allows to 
* extract information about an entity.
* A simple example of jar creation
* and usage. 
* @author Ago
*
*/

public class WikiAPI {


/** * Used to get output in json format. */ public static int FORMAT_JSON = 1; /** * Used to get output in xml format. */ public static int FORMAT_XML = 2;

public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(getWikiInformation("Main page", WikiAPI.FORMAT_JSON)); System.out.println(getWikiInformation("Estonia", WikiAPI.FORMAT_JSON)); System.out.println(getWikiInformation("Tallinn", WikiAPI.FORMAT_JSON, "info", "recentchanges"));

}


/** * Uses Wikipedia API to retrieve general information about * a certain page in XML-format. * @param pageName Page title to be searched for * @return Response in XML-format */ public static String getWikiInformation(String pageName) { return getWikiInformation(pageName, FORMAT_XML); }

/** * Uses Wikipedia API to retrieve general information about * a certain page. * @param pageName Page title to be searched for * @param format In which format the response is returned, * use WikiAPI.FORMAT_* constants. * @return Response in the specified format */ public static String getWikiInformation(String pageName, int format) { return getWikiInformation(pageName, format, "info", null); }

/** * Uses Wikipedia API to retrieve information about * a certain page. * @param pageName Page title to be searched for * @param format In which format the response is returned, * use WikiAPI.FORMAT_* constants. * @param prop Value for "prop" parameter (properties) * @param list Value for "list" parameter * @return Response in the specified format */ public static String getWikiInformation(String pageName, int format, String prop, String list) {

String formatString = "json"; if (format == WikiAPI.FORMAT_XML) { formatString = "xml"; } String listStr = ""; if (list != null) { listStr = "&list=" + list; } try { URL url = new URL("http://en.wikipedia.org/w/api.php?format=" + formatString + "&continue=" + "&action=query&titles=" + URLEncoder.encode(pageName, "UTF-8") + "&prop=" + prop + listStr); BufferedReader br = new BufferedReader( new InputStreamReader(url.openStream())); String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); } return sb.toString();

} catch (MalformedURLException e) { // TODO Auto-generated catch block return null; } catch (IOException e) { // TODO Auto-generated catch block return null; } }

}

</source>