-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathService.java
More file actions
57 lines (39 loc) · 1.29 KB
/
Service.java
File metadata and controls
57 lines (39 loc) · 1.29 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
package br.com.brasilapi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
class Service {
private static HttpsURLConnection connection;
protected Service() {
}
protected static HttpsURLConnection getHttpsURLConnection() {
return connection;
}
protected static String connection(String urlParameter) {
String json = null;
try {
URL url = new URL("https://brasilapi.com.br/api/" + urlParameter);
Log.setConsole("Acessando: " + url);
connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("GET");
if (Log.getEnable() && connection.getResponseCode() != HttpsURLConnection.HTTP_OK) {
Log.setConsoleError("ERROR. HTTP error code: " + connection.getResponseCode() + "\n");
}
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String output, retorno = "";
while ((output = br.readLine()) != null) {
retorno += output;
}
json = retorno;
Log.setConsole("Json retornado: " + json);
} catch (IOException e) {
Log.setConsoleError(e.getMessage());
//conector.disconnect();
//e.printStackTrace();
}
return json;
}
}