-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenName.java
More file actions
50 lines (34 loc) · 1.47 KB
/
OpenName.java
File metadata and controls
50 lines (34 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//import java.util.HashMap;
import java.net.*;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class OpenName {
public static void main(String[] args){
//OpenName_namer firstAttempt = new OpenName_namer("harry potter chamber of secrets");
//HashMap<String, String> metaMap = new HashMap<String, String>();
try{
String url = "http://www.omdbapi.com/";
String charset = "UTF-8";
String searchType = "?t=";
String toSearch = "harry potter and the chamber of secrets";
String apiKey = "apikey=bed3cce4"; //"INSERT_OMDB_APIKEY_HERE";
String encodedSearch = URLEncoder.encode(toSearch, charset);
String query = searchType + encodedSearch + '&' + apiKey;
System.out.println(query);
URLConnection connection = new URL(url + query).openConnection();
//URLConnection connection = new URL(url).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
// if we make it here, we got a response and no errors.
Scanner scanner = new Scanner(response);
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
scanner.close();
} catch (Exception e) {
System.out.println("Error!: " + e);
}
System.out.println("end!");
}
}