Java: Accessing a String outside of a function

I am a hobby programmer and am trying to access returnJson outside of the onSuccess function. Inside of the function, I can see that it returns as expected. Could somebody explain why I cannot view it outside and what I can do to make it accessible on the outside?

String returnJson;
client.post(context, url, entity, "application/json", new JsonHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        returnJson = response.toString();
    }
});

Log.d("get json",returnJson);

Answers


You can access it fine, but it is not initialized until the onSuccess() is called, which is likely after the Log.d() is called, due to client.post() being executed asyncronously.

Because of this, at the moment you try to log returnJson, it is not yet initialized and the logging won't work as you expect.


Need Your Help

most efficient way to send dynamic columns

c# sql .net asp.net-mvc ado.net

Pretty difficult to explain, but here goes... We are currently working with a procedure which returns dynamic columns,

How do I format Visual Studio Test results file (.trx) into a more readable format?

visual-studio-2008 unit-testing

Have just started using Visual Studio Professional's built-in unit testing features, which as I understand, uses MS Test to run the tests.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.