title: Accessing retrofit 2 data outside on response?
body: <p>I am working on two apps, in one of my app "A" i applied retrofit 2.</p>
  
  <p>This was the method i used to retrieve data.</p>
  
  <p>But here in on Response  the data retrieved in response body can be set to activity variables and can be used outside this method without getting null values.</p>
  
  <pre><code>  public void fetch_information() {
              ApiInterface = ApiClient.getApiClient().create(Api.class);
  
              Call&lt;List&lt;City&gt;&gt; call = ApiInterface.GetCities();
  
  
              call.enqueue(new Callback&lt;List&lt;City&gt;&gt;() {
                  @Override
                  public void onResponse(Call&lt;List&lt;City&gt;&gt; call, Response&lt;List&lt;City&gt;&gt; response) {
                      citylist = new ArrayList&lt;City&gt;();
                      citylist = response.body();  
                      cities = new String[citylist.size()];
                      citiesid = new String[citylist.size()];
                      for (int i = 0; i &lt; citylist.size(); i++) {
                          cities[i] = citylist.get(i).getCityName();
                          citiesid[i] = citylist.get(i).getCityId();
  
  
                      }
  
                      city_adapter = new ArrayAdapter&lt;String&gt;(Pay_Payment_X.this, android.R.layout.simple_list_item_1, cities);
                      city_adapter.setDropDownViewResource(R.layout.spinner_dropdown_layout);
                      City_Spinner.setAdapter(city_adapter);
  
  
                  }
  
  
                  @Override
                  public void onFailure(Call&lt;List&lt;City&gt;&gt; call, Throwable t) {
                      Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
                  }
              });
  
          }
  </code></pre>
  
  <p>after applying this method and on debugging this method i will retain values of varaibles "cities" and "citiesid"out side onResponse.</p>
  
  <p>But applying retrofit 2 similarly on another app "B", i did the same thing for retrieving data on different URL.</p>
  
  <pre><code>ApiUtil.getServiceClass().getAllPost().enqueue(new Callback&lt;List&lt;ApiObject&gt;&gt;() {
  
                  @Override
                  public void onResponse(Call&lt;List&lt;ApiObject&gt;&gt; call, Response&lt;List&lt;ApiObject&gt;&gt; response) {
                      if (response.isSuccessful()) {
                          List&lt;ApiObject&gt; postList = response.body();
  
  
                              try {
  
                                  for (int i = 0; i &lt; postList.size(); i++) {
                                      String Name = postList.get(i).getGamesName();
                                      mGamesName.add(Name);
                                  }
                              } catch (Exception e) {
                              }
  
                          Log.d(TAG, "Returned count " + postList.size());
                          NewAdapter adapter = new NewAdapter(getApplicationContext(), postList);
                          recyclerView.setAdapter(adapter);
                      }
                  }
  
                  @Override
                  public void onFailure(Call&lt;List&lt;ApiObject&gt;&gt; call, Throwable t) {
                      //showErrorMessage();
                      Log.d(TAG, "error loading from API");
                  }
  
              });
  </code></pre>
  
  <p>the data is retrievable inside onResponse but outside it shows null.
  So here variables are not retaining values.</p>
  
  <p>Why is this happening?</p>
  
  <p>the only thing came to mind is retrieving data can take time while your code lines are being read and finding null values as data has not been received yet.</p>
  
  <p>Also to mention in app "A" the data retrieved is huge but in app "B" only 3 objects with string values.But still in app"A" data is retrievable.</p>
  
  <p>In app 2 did this for resolving my issue.</p>
  
  <pre><code>public void doRequest( final ApiCallback callback){
  
          ApiUtil.getServiceClass().getAllPost().enqueue(new Callback&lt;List&lt;ApiObject&gt;&gt;() {
  
              @Override
              public void onResponse(Call&lt;List&lt;ApiObject&gt;&gt; call, Response&lt;List&lt;ApiObject&gt;&gt; response) {
                  if (response.isSuccessful()) {
                      List&lt;ApiObject&gt; postList = response.body();
                      callback.onSuccess(postList);
                      // apobject =response.body();
                      if(response.isSuccessful()) {
                          try {
  
                              for (int i = 0; i &lt; postList.size(); i++) {
                                  String Name = postList.get(i).getGamesName().toString();
                                  mGamesName.add(Name);
                              }
                          } catch (Exception e) {
                          }
                      }
                      Log.d(TAG, "Returned count " + postList.size());
                      NewAdapter adapter = new NewAdapter(getApplicationContext(), postList);
                      recyclerView.setAdapter(adapter);
                  }
              }
  
              @Override
              public void onFailure(Call&lt;List&lt;ApiObject&gt;&gt; call, Throwable t) {
                  //showErrorMessage();
                  Log.d(TAG, "error loading from API");
              }
  
          });
  
  
      }
  </code></pre>
  
  <p>pass an interface</p>
  
  <pre><code> public interface ApiCallback{
          void onSuccess(List&lt;ApiObject&gt; result);
      }
  </code></pre>
  
  <p>and in on Create view of activity i called this</p>
  
  <pre><code> doRequest(new ApiCallback(){
              @Override
              public void onSuccess(List&lt;ApiObject&gt; result){
                  //here i can set variable values
              }
          });
  </code></pre>