title: Async Task Fragment Background Data
body: <p>I am very new and confused as to how to load lots of template JSON data from this website, into my application without slowing down the main UI thread. I can get the data to load from the JSON response into my recycler view, but clicking on the bottom navigation tab that displays that data takes a long time to load onto the page.</p>
  
  <p>I understand that this is because data isn't being correctly loaded onto the background thread and thats where I am confused.</p>
  
  <p>I want to be able to return an ArrayList from the background task, and take that populated arraylist and pass it as my adapter for the recyclerview. Here is the fragment that contains both the AsyncTask, and the recyclerview code: </p>
  
  <pre><code>public class LoadJSONAsyncTask extends AsyncTask&lt;String, String, ArrayList&lt;String&gt;&gt;
      {
  
          protected void onPreExecute()
          {
  
  
          }
  
          public String getJSONFromURL(String inUrl) throws IOException
          {
              URL url2 = new URL(inUrl);
              HttpURLConnection urlConnection = (HttpURLConnection) url2.openConnection();
              StringBuilder total = new StringBuilder();
  
              try
              {
                  InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                  BufferedReader r = new BufferedReader(new InputStreamReader(in));
  
                  for(String line; (line = r.readLine()) != null;)
                  {
                      total.append(line).append('\n');
                  }
              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
  
              finally
              {
                  urlConnection.disconnect();
              }
  
              return total.toString();
          }
  
          @Override
          protected ArrayList&lt;String&gt; doInBackground(String... params)
          {
              LoadJSONAsyncTask jParser = new LoadJSONAsyncTask();
              JSONArray array = null;
              JSONObject json = null;
              String url = "https://baconipsum.com/api/?type=meat-and-filler";
  
              StringBuilder returnMe = new StringBuilder();
              results = new ArrayList&lt;&gt;();
  
              try {
                  array = new JSONArray(jParser.getJSONFromURL(url));
                  for(int n = 0; n &lt; array.length(); n++)
                  {
                      try {
                          json = new JSONObject();
  
                          json.put("name", jParser.getJSONFromURL(url));
                          results.add(jParser.getJSONFromURL(url));
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
  
              } catch (JSONException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
  
  
              return results;
          }
  
          protected void onPostExecute()
          {
  
          }
  
      }
  </code></pre>
  
  <p>Within my onCreateView() method in my fragment, the recyclerview is defined as follows: </p>
  
  <pre><code>@Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          try
          {
              results = new LoadJSONAsyncTask().execute(url).get();
              for(int i = 0; i &lt; results.size(); i++)
              {
                  newsFeedList.add(new NewsFeed(results.get(i)));
              }
          } catch (ExecutionException e) {
  
          } catch (InterruptedException e) {e.printStackTrace();
              e.printStackTrace();
          }
          View view = inflater.inflate(R.layout.fragment_feeds, container, false);
  
          newsFeedList = new ArrayList&lt;&gt;();
  
          recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
          recyclerView.setHasFixedSize(true);
          recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  
  
          final NewsAdapter mAdapter = new NewsAdapter(getActivity(), newsFeedList);
          recyclerView.setAdapter(mAdapter);
  
  
          return view;
      }
  </code></pre>
  
  <p>I have a feeling that inside my onCreateView() method the way I'm calling the arraylist to execute the data is what's hanging up the data on the thread. A clear explination and example would be greatly appreciated!</p>