forked from builder-of-web3/HackR_Java_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGET Http client to read JSon
More file actions
98 lines (82 loc) · 2.24 KB
/
GET Http client to read JSon
File metadata and controls
98 lines (82 loc) · 2.24 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.net.*;
import com.google.gson.*;
public class Main {
/*
* Complete the function below.
*/
static int getNumberOfMovies(String substr) throws IOException {
/*
* Endpoint: "https://jsonmock.hackerrank.com/api/movies/search/?Title=substr"
*/
int count = 0;
StringBuilder sb = new StringBuilder();
URLConnection urlConn = null;
InputStreamReader in = null;
try {
URL url = new URL("https://jsonmock.hackerrank.com/api/movies/search/?Title=maze");
urlConn = url.openConnection();
if (urlConn != null)
urlConn.setReadTimeout(60 * 1000);
if (urlConn != null && urlConn.getInputStream() != null) {
in = new InputStreamReader(urlConn.getInputStream());
BufferedReader bufferedReader = new BufferedReader(in);
if (bufferedReader != null) {
int cp;
while ((cp = bufferedReader.read()) != -1) {
sb.append((char) cp);
}
Gson gson = new Gson();
Application app = gson.fromJson(new String(sb), Application.class);
//System.out.println(app.getTotal());
bufferedReader.close();
count = app.getTotal();
}
}
in.close();
} catch (Exception e) {
// log
}
return count;
}
class Application {
private int page;
private int per_page;
private int total;
private int total_pages;
ArrayList<Object> data = new ArrayList<Object>();
// Getter Methods
public int getPage() {
return page;
}
public int getPer_page() {
return per_page;
}
public int getTotal() {
return total;
}
public int getTotal_pages() {
return total_pages;
}
// Setter Methods
public void setPage(int page) {
this.page = page;
}
public void setPer_page(int per_page) {
this.per_page = per_page;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal_pages(int total_pages) {
this.total_pages = total_pages;
}
}
public static void main(String[] args) throws IOException {
System.out.print(getNumberOfMovies("url"));
}
}