Keeping user logged in with SharedPreferences

时间:2015-08-07 01:38:33

标签: java android

I have a Java SessionManager class which contains many methods for different login actions with sharedPreferences (such as creating a login session, clearing login session, etc.). I also have a dispatch activity which is launched when the app is run. This dispatch activity calls a method from my SessionManager class to check if a user is currently logged in. If he/she is, and intent takes them to my main activity. Otherwise, the login activity is called where they can then login, etc. However, whenever I run my app, I get an error. My code for my main activity is as follows:

public class MainList extends AppCompatActivity {


RecyclerView gradeList;
SwipeRefreshLayout swipeRefreshLayout;
public SessionManager session;

static String tab1 = "studentdata";
static String tab2 = "gradebook";
static String tab3 = "weeklysummary";
static String action = "form";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_list);
    Log.e("MESSAGE: ", "In second class");

    session = new SessionManager(getApplicationContext());

    setTheme(R.style.AppTheme);


    Toolbar toolbar;
    toolbar = (Toolbar)findViewById(R.id.tb);
    setSupportActionBar(toolbar);

    new infoGetter().execute();

    gradeList = (RecyclerView)findViewById(R.id.grade_list);
    LinearLayoutManager lm = new LinearLayoutManager(getApplicationContext());
    lm.setOrientation(lm.VERTICAL);
    gradeList.setLayoutManager(lm);

    swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refreshGrades();
        }
    });


}


private void refreshGrades(){


    new infoGetter().execute();

    swipeRefreshLayout.setRefreshing(false);

}

@Override
public void onBackPressed() {
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main_list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public class Wrapper{
    public Elements gradeList;
}

private class infoGetter extends AsyncTask<Void, Void, Wrapper> {

    String studentID = session.getID();
    String username = session.getUser();
    String password = session.getPass();

    @Override
    protected Wrapper doInBackground(Void... params) {

        Wrapper w = new Wrapper();

        String loginURL = "https://parents.mtsd.k12.nj.us/genesis/parents/j_security_check";
        String userDataUrl = "https://parents.mtsd.k12.nj.us/genesis/parents?tab1=" + tab1 + "&tab2=" + tab2 + "&tab3=" + tab3 + "&studentid=" + studentID + "&action=" + action;

        Connection.Response response = new GradeFetcher().getRequest(userDataUrl);
        Document loggedInDocument = new GradeFetcher().postRequest(loginURL, username, password, studentID, response);
        Elements grades = new GradeFetcher().gradeExtractor(loggedInDocument);

        w.gradeList = grades;
        Log.e("MESSAGE: ","Going to onPostExecute");

        return w;
    }

    protected void onPostExecute(Wrapper w) {

        ArrayList<String>gl = new ArrayList<>();
        for (Element e:w.gradeList){
            gl.add(e.text());
        }

        String[]gradeData = gl.toArray(new String[gl.size()]);

        gradeList = (RecyclerView)findViewById(R.id.grade_list);
        LinearLayoutManager lm = new LinearLayoutManager(getApplicationContext());
        lm.setOrientation(lm.VERTICAL);
        gradeList.setLayoutManager(lm);
        gradeList.setAdapter(new RecyclerAdapter(gradeData));
    }
}
}

and my error is as follows:

08-06 21:34:19.970    2010-2010/com.aurum.gradebook E/MESSAGE:﹕ In second class
08-06 21:34:20.018    2010-2010/com.aurum.gradebook E/RecyclerView﹕ No adapter attached; skipping layout
08-06 21:34:20.035    2010-2010/com.aurum.gradebook E/RecyclerView﹕ No adapter attached; skipping layout
08-06 21:34:20.182    2010-2038/com.aurum.gradebook E/GET REQUEST﹕ Making GET request to gradebook page
08-06 21:34:20.183    2010-2038/com.aurum.gradebook E/AndroidRuntime﹕ FATAL              EXCEPTION: AsyncTask #1
Process: com.aurum.gradebook, PID: 2010
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:304)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)
 Caused by: java.lang.IllegalArgumentException: Data value must not be null
        at org.jsoup.helper.Validate.notNull(Validate.java:26)
        at org.jsoup.helper.HttpConnection$KeyVal.value(HttpConnection.java:884)
        at org.jsoup.helper.HttpConnection$KeyVal.create(HttpConnection.java:864)
        at org.jsoup.helper.HttpConnection.data(HttpConnection.java:131)
        at com.aurum.gradebook.GradeFetcher.postRequest(GradeFetcher.java:57)
        at com.aurum.gradebook.MainList$infoGetter.doInBackground(MainList.java:124)
        at com.aurum.gradebook.MainList$infoGetter.doInBackground(MainList.java:109)
        at android.os.AsyncTask$2.call(AsyncTask.java:292)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

            

I have checked through my code multiple times but I cannot find my error. I know it is something to do with my AsycTask my I really don't see where. All help is appreciated and will be rewarded with bounty if possible. I can provide my other code if it is necessary.

My post request:

public Document postRequest(String loginURL, String username, String password, String studentID, Response res) {

    Document doc = null;

    try {

        doc = Jsoup.connect(loginURL)
                .userAgent("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.4 Safari/537.36")
                .referrer("https://parents.mtsd.k12.nj.us/genesis/parents?gohome=true")
                .cookies(res.cookies())
                .data("j_username", username)
                .data("j_password", password)
                .post();

        Log.e("POST REQUEST", "Connecting to login page, posting credentials...");


    } catch (IOException ioe) {

        ioe.printStackTrace();

    }

    return doc;

}

1 个答案:

答案 0 :(得分:0)

I think your username or password is null, please check these values

You can download source code of jsoup at jsoup.org then find that in HttpConnection.java the following code

public KeyVal value(String value) {
        Validate.notNull(value, "Data value must not be null");
        this.value = value;
        return this;
    }