title: How can I store data from OnPostExecute()?
body: <p>I am trying to get the id numbers of foods from the <a href="https://en.wikipedia.org/wiki/United_States_Department_of_Agriculture" rel="nofollow noreferrer">USDA</a> database. My issue is that I can get the JSON data, but I can not take it out of the <code>OnPostCreate()</code> function. When I iterate through <code>JSONdataWIthIDs</code> in the <code>OnPostCreate()</code> all the data is there. But when I try to use the data someplace else, it is empty. How do I store the data into an array?</p>
  <p>Here is the constructor.</p>
  <pre><code>public USDA(String nameOfFoodToSearchFor) {
  
      setJSONdataWIthIDs(nameOfFoodToSearchFor);
  
      // This returns 0
      Log.i(&quot;TAG&quot;, &quot;SONdataWIthIDs size: &quot; + JSONdataWIthIDs.size());
  
      // This does not work.
      for(String s: JSONdataWIthIDs)
      {
          Log.i(&quot;TAG OUTPUT&quot;, s);
      }
  }
  </code></pre>
  <p>Here is the code with the OnPostCreate.</p>
  <pre><code>private void setJSONdataWIthIDs(String nameOfFood) {
  
      final String url1 = &quot;https://api.nal.usda.gov/ndb/search/?format=json&amp;q=&quot; + nameOfFood + &quot;&amp;sort=n&amp;max=25&amp;offset=0&amp;api_key=x5qM9v8PkjZrTf2cVSHzoK7y4GsSBgoQEmJsbwqV&quot;;
  
      AsyncTask asyncTask = new AsyncTask() {
          @Override
          protected Object doInBackground(Object[] objects) {
  
              OkHttpClient client = new OkHttpClient();
  
              Request request = new Request.Builder()
                      .url(url1)
                      .build();
  
              Response response = null;
  
              try {
                  response = client.newCall(request).execute();
                  return response.body().string();
              }
              catch (IOException e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(Object o) {
  
              String lines[] = o.toString().split(&quot;\\r?\\n&quot;);
  
              for(int i = 0; i &lt; lines.length; i++)
              {
                  JSONdataWIthIDs.add(lines[i]);
              }
  
              // This works
              for(String s : JSONdataWIthIDs)
              {
                  Log.i(&quot;TAG&quot;, s);
              }
          }
      }.execute();
  }
  </code></pre>