2012-07-19 66 views
1

你好早安朋友,問題與登錄/註銷funcationality

我工作的,需要login/logout funcationality應用。在這裏我登錄成功,之後,註銷也完美,但是當我嘗試again login進入應用程序,給我406 status code。這裏我使用sharedpreference作爲登錄/註銷功能。

但是,當我restart the application隨機作品意味着它可能有時登錄或有時不會。但是當close the emulator再次啓動然後工作完美。

Login.java

請檢查onPostExecute()方法,下面的代碼

@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

在主類中我有一個註銷按鈕。

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; 

在這裏,我想會的數據不清晰正確,普萊舍讓我知道我做的錯誤。還有另一種登錄/註銷解決方案,然後通知我。

謝謝。

回答

2

我不認爲你的SharedPreferences API有什麼問題。我檢查了你正在使用的Rest網站服務URL和它的一個Drupal站點。你必須先調用user.logout來註銷。因爲你正在使用REST嘗試this.I還沒有測試這一點,但它應該工作

String loginURL = "http://www.cheerfoolz.com/rest/user/logout"; 

strResponse = util.makeWebCall(loginURL,sessionid); 

你也可能要檢查,如果你如果已經啓用的應用程序/ x-WWW的形式urlencoded的內容類型是否正確配置了REST服務器endpoint.Check您的REST服務端點。從服務中的編輯資源轉到服務器。因爲在第一次調用登錄時它工作正常,所以我懷疑這可能是問題所在。但仍然檢查它。

+1

請讓我們知道這兩個解決方案中的哪一個解決了它,因爲這可能會幫助一些人有類似的問題。謝謝。 – 2012-07-19 07:32:04

+0

目前我正在使用第一個解決方案,它工作正常,我不嘗試第二個解決方案。 – 2012-07-19 09:05:15

+0

這裏我傳遞了sessionID的usrId instad – 2012-07-19 09:06:40