2

I am building an android app that is supposed to edit documents it can get from a remote server. However, to do so i need the user to log in. This should happen like this:

User types in userId and password, POST is sent with this body:

    {
     "username": "normal_username",
     "password": "normal_password"
    }

and when i do so in postman the response body looks like this { "username": "normal_username", "token": "lotsandlotsofnumbersandletters" }

I want to retrieve this token, but i dont know how to do so in android, are there any external libraries that could make this easier for me perhaps?

  • You're looking for a json deserializer. I recommend GSON. Jackson is another option. Using Android or just Java probably won't make a difference. https://stackoverflow.com/questions/2864370/how-do-i-use-googles-gson-api-to-deserialize-json-properly – Philip Rego May 08 '19 at 16:27
  • 2
    I combined GSON with the Retrofit library and got it working, thanks for the answer! – compilenoerrors May 10 '19 at 11:32

1 Answers1

-1

Since the response body is JSON, you need to parse it. Use a library such as GSON to do so then you can easily grab the value of the "token" key.

You also might want to look at a library that wraps the entire REST API. Retrofit from Square is one of the most popular libraries that does this.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268