Hello good morning friends,
I am working for the application that required login/logout funcationality. Here I have login success,After that logout also work perfect but when I try for again login into the application that give me 406 status code. here I am using sharedpreferences for the login/logout funcationality.
But when I restart the application it works random means it may sometimes login or sometimes not. But when close the emulator again start then works perfect.
Login.java
please check the
onPostExecute()method for below code
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
String loginURL = "http://www.cheerfoolz.com/rest/user/login";
strResponse = util.makeWebCall(loginURL, uName, Password);
try {
JSONObject jsonSession = new JSONObject(strResponse);
session = new SessionID();
SessionID.sessionId = jsonSession.getString("sessid");
SessionID.sessionName = jsonSession.getString("session_name");
JSONObject jsonuser=jsonSession.getJSONObject("user");
SessionID.userID = jsonuser.getInt("uid");
} catch (JSONException e1) {
e1.printStackTrace();
}
return null;
}
@Override
public void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try {
if (strResponse.substring(KEY_SUCCESS) != null) {
txterror.setText("");
SharedPreferences userDetails =getSharedPreferences("userdetails", MODE_PRIVATE);
Editor edit = userDetails.edit();
edit.putString("username", uName);
edit.putString("password", Password);
edit.commit();
} else {
txterror.setText("Username and Password Not valid !!!");
}
} catch (Exception e) {
// TODO: handle exception
}
}
Main.java
In main class I have a logout button.
case R.id.home_btn_feature_logout:
SessionID.setUserID(0);
SharedPreferences settings = getSharedPreferences("userdetails", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.remove("username");
editor.remove("password");
editor.clear();
editor.commit();
login.setVisibility(View.VISIBLE);
logout.setVisibility(View.GONE);
break;
Here I think session data is not clear properly, Plese let me know where I do mistake. And there is another solution for the login/logout then inform me.
Thank you.
I dont think there is anything wrong with your SharedPreferences API.I checked the Rest web service URL that you are using and its a Drupal site.You have to call user.logout to logout first.Since you are using REST try this.I havent tested this but it should work
Also you might want to check if you have correctly configured your REST server endpoint.Check if you have enabled application/x-www-form-urlencoded content type of your REST service endpoint.Go to Server from Edit Resources in Services.Since it is working fine when login is called the first time I doubt this might be the problem.But still check it.