android cookie 和 SharedPreferences 之间的区别

发布于 2024-12-04 18:26:01 字数 2064 浏览 0 评论 0原文

我正在尝试开发一个应用程序,它将用户名、名字、姓氏密码从我的应用程序发送到服务器(网站)。 如果该用户不存在,则注册该用户;如果存在,则登录该用户。 所有这些都运行良好。问题是我想退出,因为需要设置 cookie。

如何设置我没有得到的cookie。还是共享偏好好?

我的代码如下,

private boolean sendJsonToServer(String jsonStr) {
        Log.d("sendJsonToServer", "called");
        boolean isDataSend = false;
        try {
            HttpClient client = new DefaultHttpClient();

            HttpPost request = new HttpPost(url);

            List<NameValuePair> value = new ArrayList<NameValuePair>();

            value.add(new BasicNameValuePair("Name", jsonStr));

            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);

            request.setEntity(entity);

            HttpResponse res = client.execute(request);


            HttpEntity httpEntity = res.getEntity();
            String entityStr = convertStreamToString(httpEntity.getContent());

            JSONObject resObject = new JSONObject(entityStr);
            resObject.getString("result");
            resObject.getString("token");

            Log.e("entity", "" + entityStr + "  " + resObject.getString("result")
                            + "   " + resObject.getString("token"));

            Editor edit = prefUserDetails.edit();
            edit.clear();
            edit.putString(TOKEN, resObject.getString("token"));
            edit.commit();

            String token = prefUserDetails.getString("token", "");
            Log.e("token from sharedPreference", token);

                    String[] status_String = res.getStatusLine().toString().trim()
                .split(" ");
            if (status_String[1].equals("200")) {
                isDataSend = true;
                Log.e("isSend", "ture");
            }


        } catch (Exception e) {
            System.out.println("Exp=" + e);
        }
        return isDataSend;
    }

我将包含用户名、性别所有详细信息的 jsonObject(jsonStr) 发送到我的服务器进行注册。注册后登录,服务器给出响应,一个是结果,另一个是令牌。我想将该令牌存储到 cookie 中。

然后,如果我按下退出按钮,令牌应该从 cookie 中删除。 我在上面的函数中使用了共享偏好,但我想使用cookies。

请给我一个方法。 谢谢

I am trying to develop an application where its taking username, first name , last name password from my app send to the server(website).
if that user is not present then it signed up that user, if present then signed in to that user.
All these are working perfectly. Problem is i want to give signout, for that cookie is required to set.

How to set the cookie that i am not getting. or sharedpreference is good?

My code bellow,

private boolean sendJsonToServer(String jsonStr) {
        Log.d("sendJsonToServer", "called");
        boolean isDataSend = false;
        try {
            HttpClient client = new DefaultHttpClient();

            HttpPost request = new HttpPost(url);

            List<NameValuePair> value = new ArrayList<NameValuePair>();

            value.add(new BasicNameValuePair("Name", jsonStr));

            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);

            request.setEntity(entity);

            HttpResponse res = client.execute(request);


            HttpEntity httpEntity = res.getEntity();
            String entityStr = convertStreamToString(httpEntity.getContent());

            JSONObject resObject = new JSONObject(entityStr);
            resObject.getString("result");
            resObject.getString("token");

            Log.e("entity", "" + entityStr + "  " + resObject.getString("result")
                            + "   " + resObject.getString("token"));

            Editor edit = prefUserDetails.edit();
            edit.clear();
            edit.putString(TOKEN, resObject.getString("token"));
            edit.commit();

            String token = prefUserDetails.getString("token", "");
            Log.e("token from sharedPreference", token);

                    String[] status_String = res.getStatusLine().toString().trim()
                .split(" ");
            if (status_String[1].equals("200")) {
                isDataSend = true;
                Log.e("isSend", "ture");
            }


        } catch (Exception e) {
            System.out.println("Exp=" + e);
        }
        return isDataSend;
    }

I am sending jsonObject(jsonStr) which consists of username,gender all details to my server for signup.After signup it signed in and server gives response, one is result and another is token. I want to store that token into cookie.

Then if i press sign out button the token should be remove from cookie.
I am used here shared preference in the above function, but i want cookies to use.

Plz give me a way.
thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-12-11 18:26:01

当我们谈论 cookie 时,SharedPreference 是一个不错的选择。我认为您将在 cookie 中存储与会话相关的日期,并且共享首选项很好地支持此功能。

SharedPreference is good option when we aretalking about cookie. As i think you will be storing session related date in cookie and this feature is well supported by shared preference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文