android - Creating and Moving Shared Preference Method from Activity to Java -


i'm still new android studio , want make shared preference method in device.java class validates correctly. however, i'm not sure variables should initialise , code placement. share preference code validates user same user , performslogin bypass login screen i.e. first time requires login, life time bypasses welcome screen.

sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(loginactivity.this);      if (sharedpreferences.contains("ip") && sharedpreferences.contains("username") && sharedpreferences.contains("password")) {         string strusername=sharedpreferences.getstring("username", username);         string strpassword=sharedpreferences.getstring("password", password);         string stripaddress=sharedpreferences.getstring("ip", ipaddress);         performlogin(strusername, strpassword,stripaddress);     } 

java class:

public static void login(string username, string password, string ipaddress, final callback callback) throws jsonexception {      okhttpclient client = new okhttpclient();      jsonobject jsonobject = new jsonobject();     jsonobject.put("username", username);     jsonobject.put("password", password);      requestbody body = requestbody.create(mediatype.parse("application/json"), jsonobject.tostring());     request request = new request.builder()             .url("http://"+ipaddress+"/api/v0/login")             .post(body)             .build();      client.newcall(request).enqueue(new com.squareup.okhttp.callback() {           // produces exception if db connection request has fail         @override         public void onfailure(request request, ioexception e) {             callback.onloginfailure(e);         }          // checks db request passed or fail         @override public void onresponse(response response){             if (!response.issuccessful()) {                 callback.onloginfailure(new ioexception("unexpected code " + response));                 return;             }              string jsonasstring = null;              try {                 jsonasstring = response.body().string();                  jsonobject json = new jsonobject(jsonasstring);                  if (json.getstring("status").equals("ok")){                      device device = new device();                     device.locationid = json.getint("location_id");                     //device.imageid = json.getint("imageid");                    // device.imagename = json.getstring("imagename");                      device.id = json.getint("id");                      device.instance = device;                      callback.onloginsuccess(device);                 } else {                     throw new jsonexception("invalid service response");                 }              } catch (exception e) {                 e.printstacktrace();                 callback.onloginfailure(e);             }         }     });  } 

in device.java can add this:

public class device {     private final sharedpreferences preferences;      public device(context context) {         preferences = preferencemanager.getdefaultsharedpreferences(context);     }      public void validatelogin(string username, string password, string ipadress) {                 if (preferences.contains("ip") && preferences.contains("username") && preferences.contains("password")) {             string strusername = preferences.getstring("username", username);             string strpassword = preferences.getstring("password", password);             string stripaddress = preferences.getstring("ip", ipaddress);             performlogin(strusername, strpassword,stripaddress);         }     }  // code... } 

then loginactivity can call:

device device = new device(this); device.validatelogin(); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -