title: Callback function in Android
body: <p>I am using Volley to make Http requests to my Web Api. 
  However I am having trouble getting the values from my api calls due to the asynchronous nature of Volley. 
  I have read that using a callback function could help with this issue, however I do not know how to implement such a solution. </p>
  
  <p>How would I go about implementing a callback function in the following scenario?</p>
  
  <pre><code>public class Main
  {
     String name;
  
     WebServiceConnections wsc = new WebServiceConnections();
     name = wsc.getNameFromWeb();
  
     System.out.println("Name: " + name);
  }  
  
  
  
  public class WebServiceConnections
  {
      public String getNameFromWeb()
      {
          String url = "http://nameservice.net/GetName";
  
          JsonArrayRequest req = new JsonArrayRequest(url,
              new Response.Listener&lt;JSONArray&gt;() {
                  @Override
                  public void onResponse(JSONArray response) {
                      try {
                          return response.getString("Name");
                      }
                      catch (JSONException e) {
                          e.printStackTrace();
                      }
                  }
              }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                  VolleyLog.d(TAG, "Error: " + error.getMessage());
              }
          });
      }
  }
  </code></pre>
  
  <p>The problem with this code is that the variable "name" in Main will be null when it is called by the print statement as the asynchronous method in the WebServiceConnections class will not be finished by time the print statement is called. </p>
  
  <p>Is a callback a good way to solve this problem?</p>