简体   繁体   中英

java work around to access a variable from anonymous inner class not working

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

You're basically returning token[0] before you assign anything to it. The way that method works is like this:

You create token[0] (which is null ) at ths point. You send the request. You return token[0] (still null at this point) You get the response from the request, which has the value you initially wanted for token[0] .

Unless you get the response back, token[0] will be null . You won't be able to return it from that method. Instead I'd just make it void and wait for the request to finish. You can Toast it from onResponse if you wish.

public void getAccessToken(String url) {
    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]);
                        // do some other stuff with token[0], like toast or whatever
                    } 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM