2

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];
    }
Community
  • 1
  • 1
Aparichith
  • 1,452
  • 4
  • 20
  • 40
  • How are you planning to return the token before the request completes? Currently, you create the array (which contains null by default), you create the request, you add the request to the queue, and then you return the contents of the array (still null!). A few milliseconds later, presumably you get around to looking in the queue and actually doing the request. – user253751 Apr 15 '15 at 08:06
  • @immibis I am using `Volly` library to make a post request. By the time it enters `onResponse` method it will have the value. And i can able to set that value to a `TextView` (tv). But not able to assign and return that value outside the `onResponse` method. – Aparichith Apr 15 '15 at 09:07
  • Well yeah, because you're trying to get the value before `onResponse` is called. – user253751 Apr 15 '15 at 09:08
  • so how to make that happen.? – Aparichith Apr 15 '15 at 09:09
  • i didn't understood one thing. I am assigning a variable `taken[0]` which has a scope within `onResponse` method also, and using that variable to set it to a `Toast`. It means `token[0]` has a value assigned already. But when i try to print the same variable outside of that method it is null. How came value is null after an assignment.? – Aparichith Apr 15 '15 at 09:14
  • Experiment: what do you think this method will print? http://pastebin.com/316e1JL2 (don't actually run it; that's cheating) – user253751 Apr 15 '15 at 09:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75268/discussion-between-immibis-and-h-app-y). – user253751 Apr 15 '15 at 09:18
  • @h.APP.y just wondering, why you don't put the `Toast.makeText` function call inside the `onResponse()` ? – kucing_terbang Apr 15 '15 at 09:53
  • @kucing_terbang Actuall intention is to return `token[0]` at the end. I mean `Toast` is just to tell `token[0]` don't have value outside `onResponse`. – Aparichith Apr 15 '15 at 10:35
  • @h.APP.y yeah, i understand that, but the value of the token[0] is obtained asynchronously, which mean that the value is may not be obtained yet when the `Toast` function is called. So, why not to put it inside the `onResponse()` as it will be called after the app got the response? – kucing_terbang Apr 15 '15 at 11:15

1 Answers1

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);
}
async
  • 1,537
  • 11
  • 28