如何从JSON检索文本

时间:2019-04-01 09:52:45

标签: android

我正在尝试从JSON响应中检索令牌。我已经成功地获得了答复,但是我不知道如何检索令牌。

这是我的代码

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

以及此json的响应

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

如何仅检索request_token

3 个答案:

答案 0 :(得分:1)

尝试这个@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.

答案 1 :(得分:1)

您可以通过这种方式访问​​令牌字符串

String token = response.optString("request_token");

答案 2 :(得分:1)

您可以尝试这样

try{
     Log.i("jjj", response.toString());
     String requestToken = response.getString("request_token");
   }
catch (Exception e) {
       e.printStackTrace();
}