登录/注销功能问题

时间:2012-07-19 05:20:54

标签: android sharedpreferences

大家好,早上好,

我正在为需要login/logout功能的应用程序工作。在这里我登录成功,之后注销也很完美,但当我尝试again login进入给我406 status code的应用程序时。这里我使用sharedpreference s来登录/注销功能。

但是当我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;

这里我认为会话数据不清楚,请告诉我错误的地方。登录/注销还有另一种解决方案,然后通知我。

谢谢。

1 个答案:

答案 0 :(得分:2)

我认为您的SharedPreferences API没有任何问题。我检查了您正在使用的Rest Web服务URL及其Drupal站点。您必须先调用user.logout注销。因为您正在使用REST尝试此操作我没有对此进行测试,但它应该可行

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

strResponse = util.makeWebCall(loginURL,sessionid);

此外,您可能需要检查是否已正确配置REST服务器端点。检查是否已启用REST服务端点的application / x-www-form-urlencoded内容类型。从“服务中的编辑资源”转到“服务器”。因为它在第一次调用登录时工作正常,我怀疑这可能是问题。但仍然检查它。