-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGOT_API.java
More file actions
84 lines (64 loc) · 2.45 KB
/
GOT_API.java
File metadata and controls
84 lines (64 loc) · 2.45 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import org.json.JSONObject;
import org.json.JSONArray;
import java.io.*;
import java.net.*;
public class GOT_API {
public JSONObject getObject(String str){
StringBuffer content = getJSONObject(str);
JSONObject json = new JSONObject(content.toString());
return json;
}
public JSONArray getArray(String str){
StringBuffer content = getJSONArray(str);
JSONArray json = new JSONArray(content.toString());
return json;
}
public StringBuffer getJSONObject(String str) {
try {
URL url = new URL(str);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(con.getInputStream());
BufferedReader br = new BufferedReader(isr);
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = br.readLine()) != null) {
content.append(inputLine);
}
br.close();
return content;
} else {
System.out.println("Error");
System.out.println("Server responded with: " + con.getResponseCode());
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return null;
}
public StringBuffer getJSONArray(String str) {
try {
URL url = new URL(str);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(con.getInputStream());
BufferedReader br = new BufferedReader(isr);
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = br.readLine()) != null) {
content.append(inputLine);
}
br.close();
return content;
} else {
System.out.println("Error");
System.out.println("Server responded with: " + con.getResponseCode());
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return null;
}
}