1

I'm trying to retrieve a token from a JSON response. I've successfully managed to get a response, but I don't know how to retrieve the token.

Here's my code

private void TmdbAuth() {

    final String TokenUrl = "https://api.themoviedb.org/3/authentication/token/new?api_key=<<api key goes here>>";
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
            Request.Method.GET,
            TokenUrl,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        Log.i("jjj", response.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_SHORT).show();
                }
            }
    );
    requestQueue.add(jsonObjectRequest);
}

and the response of this json

{"success":true,"expires_at":"2019-04-01 10:49:44 UTC","request_token":"request token goes here"}

How do I retrieve only the request_token

tony
  • 466
  • 6
  • 22

3 Answers3

1

Try this @tony

JsonObject object = new JsonObject(response); //obtained response from the server.
String request_token = object.getString("request_token");
Log.e("request_token",request_token); //Just for checking in the logcat.
Brahma Datta
  • 1,102
  • 1
  • 12
  • 20
1

You can access token String this way

String token = response.optString("request_token");
Amine
  • 2,241
  • 2
  • 19
  • 41
1

You can try like this

try{
     Log.i("jjj", response.toString());
     String requestToken = response.getString("request_token");
   }
catch (Exception e) {
       e.printStackTrace();
}
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29