Issue
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.
I understand that this is because data isn't being correctly loaded onto the background thread and thats where I am confused.
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:
public class LoadJSONAsyncTask extends AsyncTask<String, String, ArrayList<String>>
{
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<String> 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<>();
try {
array = new JSONArray(jParser.getJSONFromURL(url));
for(int n = 0; n < 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()
{
}
}
Within my onCreateView() method in my fragment, the recyclerview is defined as follows:
@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 < 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<>();
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;
}
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!
Solution
Override the onPostExecute
of your LoadJSONAsyncTask
and put all your UI update in there.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_feeds, container, false);
newsFeedList = new ArrayList<>();
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);
LoadJSONAsyncTask asyncTask = new LoadJSONAsyncTask() {
@Override
void onPostExecute(ArrayList<String> results) {
// update your adapter here with the result
for(int i = 0; i < results.size(); i++)
{
newsFeedList.add(new NewsFeed(results.get(i)));
}
// notify adapter here to update the view
mAdapter.notifyDataSetChanged()
}
}
asyncTask.execute(url);
return view;
}
.get()
hangs up your main thread.
Here's how to properly use an AsyncTask
. https://developer.android.com/reference/android/os/AsyncTask
Answered By - Israel dela Cruz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.