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!

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.