Android Open Source - SharedUrlList Request Token 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;
//from   w w w .  j  a va  2s.  c o  m
import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;

class RequestTokenTask extends AsyncTask<Void, Void, String>{
  private MainActivity mainActivity;
  private String url;
  private SharedPreferences sharedPrefs;
  
  public RequestTokenTask(MainActivity mainActivity, String password){
    this.mainActivity = mainActivity;
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mainActivity);
    String serverurl = sharedPrefs.getString("pref_serverurl", "");
    String username = sharedPrefs.getString("pref_username", "");
    String device = sharedPrefs.getString("pref_devicename", "");
    this.url=serverurl + "?api=true&tokenrequest=true&user="+Uri.encode(username)+"&password="+Uri.encode(password)+"&device="+Uri.encode(device);
  }
  @Override
  protected void onPreExecute() {
    this.mainActivity.infoMessage("requesting token ...");
  }
  @Override
  protected String doInBackground(Void... params) {
    String json_input="";
    try {
      json_input = Util.loadFromURL(this.mainActivity, this.url);
    } catch (LoadException e) {
      return e.getError();
    }
    try {
      JSONObject json = new JSONObject(json_input);
      if(json.getString("status").equals("success")){
        sharedPrefs.edit().putString("pref_token", json.getString("token")).commit();
      }else{
        return json.getString("errormessage");
      }
    } catch (JSONException e) {
      return "JSON error";
    }
    return null;
  }
  @Override
  protected void onPostExecute(String errors) {
    if(errors == null){
      mainActivity.infoMessage("token created.");
      mainActivity.updateLists();
    }else{
      mainActivity.errorMessage(errors);
      
    }
    super.onPostExecute(errors);
  }
  
}




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