-2
Good evening guys! I’m looking to develop a program for my work that will make it much easier to do what my boss asked. The problem is that I stopped at a part of the code. I already searched on the internet but I could not pass level. Someone help me please?
The problem itself: I want to make a request inside Java.Net.Http but I don’t know how to ask for the parameters inside the code Answer.body. In other bindings I would use "[]", but this does not work. The name of json parameter I need is 'phone'
Thank you from now on to everyone. Thank you very much indeed.
CLASSE APISocio.java
package info.theocastro.linkedinphoneextractor;
import java.net.URI;
import java.net.http.HttpRequest;
public class APISocio {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://rapidapi.p.rapidapi.com/cnpj/11754156000265"))
.header("x-rapidapi-key", "cf2558ca2dmsh3540bbcccf44852p166e0ejsn4cb6bd56b328")
.header("x-rapidapi-host", "consulta-empresa-cnpj-e-socios.p.rapidapi.com")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
}
CLASSE APIEmpresa.java
package info.theocastro.linkedinphoneextractor;
import java.net.URI;
import java.net.http.HttpRequest;
public class APIEmpresa {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://rapidapi.p.rapidapi.com/empresa/CYTH%20CAR%20COMERCIO%20DE%20VEICULOS%20EIRELI"))
.header("x-rapidapi-key", "cf2558ca2dmsh3540bbcccf44852p166e0ejsn4cb6bd56b328")
.header("x-rapidapi-host", "consulta-empresa-cnpj-e-socios.p.rapidapi.com")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
}
CLASSE Principal.java
package info.theocastro.linkedinphoneextractor;
import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
public class Principal{
public static void main(String[] args) throws IOException, InterruptedException {
APIEmpresa apiEmpresa = new APIEmpresa();
APISocio apiSocio = new APISocio();
HttpResponse<String> responseEmpresa = HttpClient.newHttpClient().send(apiEmpresa.request, HttpResponse.BodyHandlers.ofString());
//System.out.println(responseEmpresa.body());
HttpResponse<String> responseSocio = HttpClient.newHttpClient().send(apiSocio.request, HttpResponse.BodyHandlers.ofString());
System.out.println(responseSocio.body());
}
}
Got it. But Voce then knows how I can have the variable called 'phone' api'?
– Theo2017BR
For example, with the org.json library you create one
JSONObject obj = new JSONObject(response.getBody())
and takes the field for exampleString phone = obj.getString("phone")
. It is a matter of researching how to do exactly.– Piovezan
I edited my answer stating that it is an external library to your project.
– Piovezan