How to load data

1 vote · 1 comment

For example load data, parse it as xml, json or etc. and get list of urls

raw ·
copy
· download
// -------- Fragment ---------- public class ImagesFragment extends Fragment implements LoaderCallbacks<List<String>> { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // create and auto start loader getLoaderManager().initLoader(0, null, this); } @Override public Loader<List<Image>> onCreateLoader(int id, Bundle args) { // create own loader, for memory safety do not pass Activity reference return new UrlLoader(getActivity().getApplicationContext()); } @Override public void onLoadFinished(Loader<List<String>> arg0, List<String> arg1) { // update UI based on result of urls: arg1 } @Override public void onLoaderReset(Loader<List<String>> arg0) { // clear UI } } // ---------- Loader -------------- public final class UrlLoader extends AsyncTaskLoader<List<String>> { private List<String> urls; public ImagesLoader(Context context) { super(context); } @Override protected void onStartLoading() { // just make sure if we already have content to deliver if (urls != null) deliverResult(urls); // otherwise if something has been changed or first try if (takeContentChanged() || urls == null) forceLoad(); } @Override protected void onStopLoading() { cancelLoad(); } @Override protected void onReset() { super.onReset(); onStopLoading(); // clear reference to object // it's necessary to allow GC to collect the object // to avoid memory leaking urls = null; } private static String streamToString(InputStream stream) { Writer writer = new StringWriter(); InputStreamReader input = new InputStreamReader(new BufferedInputStream(stream), "UTF-8"); try { final char[] buffer = new char[1024]; int read; while ((read = input.read(buffer)) != -1) writer.write(buffer, 0, read); } finally { input.close(); } return writer.toString(); } @Override public List<String> loadInBackground() { // even if fail return empty list and print exception stack trace List<String> result = new ArrayList<String>(); // Do not use HttpClient, see http://android-developers.blogspot.com/2011/09/androids-http-clients.html try { HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://my.super.website.com/get/json/response").openConnection(); // by default it's a GET request try { InputStream input = new BufferedInputStream(httpURLConnection.getInputStream()); String jsonResponse = streamToString(input); // parse somehow... result.add("http://my.super.website.com/some/url/to/image"); result.add("http://my.super.website.com/some/url/to/something/else"); } finally { httpURLConnection.disconnect(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return Collections.unmodifiableList(result); } }
Add a comment

1 Comment

I think there's an error here: public Loader> onCreateLoader(int id, Bundle args) { It should be public Loader> onCreateLoader(int id, Bundle args) {

Reply · March 5, 2013, 3:03 p.m.