How to request parameters within Java

Asked

Viewed 95 times

-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());

    }

}

1 answer

0

The body (body) of the HTTP response is usually treated by the library as a string, not necessarily of a JSON, could be of an XML for example, suddenly it could even be a binary (non-textual) data. If you know you are a JSON (you have a header item that says so) you can deal with a JSON library such as org.json.

This library has to be added to your project as it is not part of the standard Java library.

  • Got it. But Voce then knows how I can have the variable called 'phone' api'?

  • For example, with the org.json library you create one JSONObject obj = new JSONObject(response.getBody()) and takes the field for example String phone = obj.getString("phone"). It is a matter of researching how to do exactly.

  • I edited my answer stating that it is an external library to your project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.