0
I need to make an API that captures some data from the machine (OS name, cpu usage, network usage etc.) and send as JSON. I was able to capture the data and have it displayed on the console in the form of JSON, however, I never messed with the data SEND. I did some research on Httprequest, but I didn’t understand how to use it. Remembering, I’ve only been dealing with Java for about 3 months, so if you can help me understand how I can do this sending, I really appreciate it!
Guys, that’s the code I’m in right now. I’m returning a STRING with the CPUINFO class Json, but if some application "pulls" it, it will pull in String format, and not Json, right? I have how to make it return in Json?
public class ServerHTTP {
static CPUINFO infocpu = new CPUINFO();
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(9080), 0);
System.out.println("Executando");
server.createContext("/teste", new MyHandler());
server.createContext("/", new HomeHandler());
server.createContext("/dados", new dataHandler());
server.setExecutor(null);
server.start();
}
static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
Headers h = t.getResponseHeaders();
h.add("Content-Type", "application/json");
String response = "<h1> Hello World! </h1>";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
static class HomeHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
Headers h = t.getResponseHeaders();
h.add("Access-Control-Allow-Origin", "*");
h.add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
String response = "<h> Homepage </h>";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
static class dataHandler implements HttpHandler {
public void handle(HttpExchange he) throws IOException {
Headers h = he.getResponseHeaders();
h.add("Access-Control-Allow-Origin", "*");
h.add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
String response = infocpu + "";
he.sendResponseHeaders(200, response.length());
OutputStream os = he.getResponseBody();
os.write(response.toString().getBytes());
os.close();
}
}
Hello. Post what you have achieved so far, with part of the code and where exactly is the doubt that the community will be happy to help :)
– David Alves
Exactly as the colleague said, post the data you have already obtained and where you need help.
– Marquin Ferreira
So, it’s because what I understood so far was very theoretical, I don’t really know how to use Httprequest, and well, to confess I’m kind of lost in this request and the like. I only know I have to send the data in Json, but tbm I’m kind of without a north. What I have is more or less that (posted in the question)
– user114945
This request you want to send is a POST? If yes, you know what data you expect to receive (Json, XML, Form Data, etc)?
– Dherik
To understand how an HTTP request works, see this link
– StatelessDev
Hi! It’s a JSON, but I have no idea how I’m going to send it :S
– user114945
And, by reading, I will not send a request, I will receive a request and send the JSON as a response by POST. It is more or less that
– user114945