0
public void getToken(String userID) throws URISyntaxException, IOException {
    URIBuilder userBuild = new URIBuilder("https://api/token");
    HttpPost post = new HttpPost(userBuild.build());
    printPost(post);
}

public void printPost(HttpPost post) throws IOException {

        HttpResponse response = client.execute(post);

        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        String line;
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
}

So, what I want to achieve is that I would like to save the "token" as a variable.

For example, when I run the method, "getToken()", it prints out like: {"token": "1234567" } on the console.

However, how sould I save that "1234567"? Is it related to something like getHeaders()?

Thanks in advance!

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
yoo seung
  • 25
  • 3

2 Answers2

0

Instead of printing the response you should parse it as JSON and store this value in a variable: https://stackoverflow.com/a/22624801/5790043 You can use Gson or Jackson libraries for it.

0

You might need to have a look at serialization and deserialization. In this case, you want to deserialize your JSON output and turn it into a Java Object. This way you can easily have access to anything you want regarding your response.

A common way to do this in Java is by using the Jackson ObjectMapper. Try to turn your response into a Java object and get the value you need.

wolfhunter
  • 33
  • 7