-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.java
More file actions
81 lines (64 loc) · 1.88 KB
/
HttpClient.java
File metadata and controls
81 lines (64 loc) · 1.88 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
package lu.cct.profileproject;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by Eddy C. Borera on 4/12/15.
* HTTPClient program for HTTP GET/POST
* request to get/post JSON data to
* a back end server.
*/
public class HttpClient
{
private URL main_url;
// ------------------------
// Overloading constructor
// ------------------------
public HttpClient(String url)
{
try {
main_url = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
// --------------------------
// Retrieve data using GET
// --------------------------
public String getData()
{
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
HttpURLConnection connection = (HttpURLConnection) main_url.openConnection();
connection.setRequestMethod("GET");
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ( (line = reader.readLine()) != null) {
sb.append(line);
}
reader.close(); // closing the buffer
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
// -------------------------
// Post JSON data to an URL
// -------------------------
public void postData()
{
}
// ------------------------
// Testing the class
// ------------------------
public static void main(String [] args)
{
HttpClient client = new HttpClient("http://66.147.235.233:3000/api/news");
System.out.println(client.getData());
}
}