Android Open Source - SharedUrlList Load Url List Task






From Project

Back to project page SharedUrlList.

License

The source code is released under:

GNU General Public License

If you think the Android project SharedUrlList listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.laxu.apps.sharedurllist;
// w w w  .j  a v  a 2s. com
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;

class LoadException extends Exception{
  private static final long serialVersionUID = 1L;
  private String error;
  public LoadException(String error) {
    this.error = error;
  }
  public String getError(){
    return this.error;
  }
}

class LoadUrlListTask extends AsyncTask<Void, Void, String> {

  private final MainActivity mainActivity;
  private String url; 
  /**
   * @param mainActivity
   */
  LoadUrlListTask(MainActivity mainActivity) {
    this.mainActivity = mainActivity;
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mainActivity);
    String serverurl = sharedPrefs.getString("pref_serverurl", "");
    String token = sharedPrefs.getString("pref_token", "");
    this.url=serverurl + "?api=true&token="+Uri.encode(token);
  }

  private String oldHostname;

  @Override
  protected String doInBackground(Void... params) {
    int currentPage = mainActivity.mViewPager.getCurrentItem();
    if (MainActivity.hostnames.size() - 1 >= currentPage) {
      oldHostname = MainActivity.hostnames.get(currentPage);
    } else {
      oldHostname = "";
    }
    String json_input;
    try{
      json_input = Util.loadFromURL(mainActivity, url);
      mainActivity.urllists_json = json_input; // cache the valid json.
      return LoadUrlListTask.parse_urllists(json_input);
    } catch (JSONException e) {
      return "JSON Error";
    }catch (LoadException e) {
      return e.getError();
    }
  }
  protected static String parse_urllists(String json_input) throws JSONException{
    JSONObject json = new JSONObject(json_input);
    if (!json.get("status").equals("success")) {
      String errormessage = json.getString("errormessage");
      return "Server Error: " + errormessage;
    }
    JSONArray hosts = (json.getJSONArray("hosts"));
    MainActivity.hostnames = new ArrayList<String>();
    for (int i = 0; i < hosts.length(); i++) {
      JSONObject host = hosts.getJSONObject(i);
      String hostname = host.getString("hostname");
      MainActivity.hostnames.add(hostname);
      JSONArray json_urls = host.getJSONArray("urls");
      ArrayList<UrlListEntry> url_list = new ArrayList<UrlListEntry>();
      for (int j = 0; j < json_urls.length(); j++) {
        JSONObject json_url = (JSONObject) json_urls.get(j);
        int id = json_url.getInt("id");
        String link = json_url.getString("link");
        String created = json_url.getString("created");
        url_list.add(new UrlListEntry(id, link, created));
      }
      MainActivity.hostURLList.put(hostname, url_list);
    }
    java.util.Collections.sort(MainActivity.hostnames);
    return null;
  }

  @Override
  protected void onPreExecute() {
    mainActivity.infoMessage("updating ...");
  };

  @Override
  protected void onPostExecute(String errormessage) {
    if (errormessage == null) {
      LinearLayout layout = (LinearLayout) mainActivity
          .findViewById(R.id.mainLayout);
      layout.invalidate();
      // mSectionsPagerAdapter = new
      // SectionsPagerAdapter(getSupportFragmentManager());
      mainActivity.mViewPager = (ViewPager) mainActivity
          .findViewById(R.id.pager);
      mainActivity.mViewPager
          .setAdapter(mainActivity.mSectionsPagerAdapter);
      mainActivity.mSectionsPagerAdapter.notifyDataSetChanged();
      int newPage = MainActivity.hostnames.indexOf(oldHostname);
      if (newPage != -1) {
        mainActivity.mViewPager.setCurrentItem(newPage);
      }
      mainActivity.hideMessage();
    } else {
      mainActivity.errorMessage(errormessage);
    }
  }

}




Java Source Code List

de.laxu.apps.sharedurllist.AddUrlDialog.java
de.laxu.apps.sharedurllist.DeleteUrlTask.java
de.laxu.apps.sharedurllist.EnterPasswordFragment.java
de.laxu.apps.sharedurllist.LoadUrlListTask.java
de.laxu.apps.sharedurllist.MainActivity.java
de.laxu.apps.sharedurllist.RequestTokenTask.java
de.laxu.apps.sharedurllist.SettingsActivity.java
de.laxu.apps.sharedurllist.ShareURLFragment.java
de.laxu.apps.sharedurllist.UrlListEntry.java
de.laxu.apps.sharedurllist.Util.java
de.laxu.apps.sharedurllist.listeners.OnAddUrlMenuItemClickListener.java
de.laxu.apps.sharedurllist.listeners.OnCopyMenuItemClickListener.java
de.laxu.apps.sharedurllist.listeners.OnDeleteMenuItemClickListener.java
de.laxu.apps.sharedurllist.listeners.OnRefreshMenuItemClickListener.java
de.laxu.apps.sharedurllist.listeners.OnRequestTokenMenuItemClickListener.java
de.laxu.apps.sharedurllist.listeners.OnSettingsButtonClickListener.java
de.laxu.apps.sharedurllist.listeners.OnSettingsMenuItemClickListener.java
de.laxu.apps.sharedurllist.addUrlTask.java