Sorry this is a repeated question ,
though referring to this post. I have tried to assign a value from anonymous inner class. But it always prints null[Toast.makeText(getBaseContext(),token[0],Toast.LENGTH_SHORT).show();
] . Where i am doing wrong in this code sample.
Is there a better way to return the string value than this.?
public String getAccessToken(String url) {
final String[] token = new String[1];
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
try {
token[0] = response.getString("access_token");
tv.setText(token[0]);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", String.valueOf(error));
}
}
);
queue.add(postRequest);
Toast.makeText(getBaseContext(),token[0],Toast.LENGTH_SHORT).show();
return token[0];
}