1

你好,朋友们早安,

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

但是当我restart the application它随机工作时,意味着它有时可能登录或有时不登录。但是,当close the emulator再次开始时,它就完美了。

登录.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
            }
    }

主.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;

在这里我认为会话数据没有正确清除,请让我知道我在哪里做错了。还有另一种登录/注销的解决方案,然后通知我。

谢谢你。

4

1 回答 1

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 内容类型。从服务中的编辑资源转到服务器。因为它是第一次调用登录时工作正常我怀疑这可能是问题。但仍然检查它。

于 2012-07-19T06:16:57.393 回答