Sketch Engine
  • Login
  • Wiki
  • Timeline
  • View Tickets
  • New Ticket
  • Search
  • Settings

SkE/Methods: example2.java

File example2.java, 2.2 kB (added by vojta, 6 months ago)
Line 
1 package jsonexample;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.io.BufferedReader;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.net.Authenticator;
9 import java.net.PasswordAuthentication;
10 import java.net.URL;
11 import java.util.Set;
12 import org.json.*;
13
14 /**
15  * @author Vojtech Kovar, xkovar3@fi.muni.cz
16  */
17
18 public class Main {
19    
20     public Main() {
21     }
22    
23     public static void main(String[] args) {
24         String data, url_string;
25         String base_url = "http://beta.sketchengine.co.uk/auth/corpora/run.cgi/";
26         String method = "wordlist";
27         Map attrs;
28         JSONObject json_query;
29        
30         final String usr = "<username>";
31         final String passwd = "<password>";
32        
33             // authentication issues
34         Authenticator auth = new Authenticator() {
35             protected PasswordAuthentication  getPasswordAuthentication () {
36                 return new PasswordAuthentication(usr, passwd.toCharArray());
37             }
38         };
39         Authenticator.setDefault(auth);
40        
41         // creating query string
42         attrs = new HashMap();
43         attrs.put("corpname", "preloaded/bnc");
44         attrs.put("wlattr", "word");
45         attrs.put("wlpat", "test.*");
46         attrs.put("format", "json");
47         json_query = new JSONObject(attrs);
48         url_string = base_url + method + "?json=" + json_query.toString();
49         System.out.println(url_string);
50        
51         try {
52                 // connecting the SketchEngine Server
53             URL url = new URL(url_string);
54             InputStream stream = url.openStream();
55             InputStreamReader isr = new InputStreamReader(stream);
56             BufferedReader reader = new BufferedReader(isr);
57            
58                 // json data receiving
59             data = reader.readLine(); // json data are on the first line
60             
61                 // now, in the 'data' variable, there is a json string
62                 // that can be parsed for json syntax
63             JSONObject json = new JSONObject(data);
64             System.out.println(json.toString(3));
65         }
66         catch(Exception e) {
67             e.printStackTrace();
68         }
69     }
70    
71 }

Download in other formats:

  • Plain Text
  • Original Format

Sketch Engine
Bringing Corpora to the Masses

Lexical Computing Ltd

Brought to you by
Lexical Computing Ltd