【问题标题】:Maintain the session after the Android app connect to web serviceAndroid 应用连接到 Web 服务后保持会话
【发布时间】:2012-10-15 04:56:52
【问题描述】:

我正在使用 ksoap2 进行 Web 服务方法调用。首先,用户必须登录。我希望 Web 服务中的其他方法只能在登录后调用。但我不能那样做。因为我在网络服务中没有任何会话。我不知道如何在网络服务中做到这一点。 Web 服务如何知道用户已经登录 Android 应用程序?您有任何想法、会话或 cookie :(。感谢您阅读我的问题。这是我在网络服务中的代码

public class Login {
String message;
Connection conn;
String url;
ResultSet rs;
Statement stmt;
public String login(String username, String password) {
    try {
        Class.forName("org.postgresql.Driver");
        url = "jdbc:postgresql://localhost:5432/FirstDB";
        conn = DriverManager.getConnection(
                url, "postgres",
                "root");
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT * From account Where username = '"+ username + "'and password = '" + password +"'");
        if ( rs.next() ) {
            message = "true";
        } else {
            message = "false";
        }
    } catch (Exception e) {
        System.err.println("Got an exception! ");
        System.err.println(e.getMessage());
    }
    return message;
}

public String Test() {
    if(message.equals("true")) 
    {
        return "abc";
    }
    return "bdf";
}

}

在安卓应用中。首先我调用方法 login(),然后调用方法 Test()。结果总是“bdf”。

【问题讨论】:

    标签: android


    【解决方案1】:

    要在 Android 应用程序中维护会话,您可以使用共享首选项。

    例如见Android User Session Management using Shared Preferences

    或者你可以这样试试Maintaining session in android ( application stay authenticated on the server side)

    【讨论】:

    • 谢谢,但我想将会话保留在 Web 服务中,而不是 android 应用程序中的 Shared Preferences :(。您还有其他想法吗?
    • link 这个题目的问题和我的问题类似。但答案尚不清楚。而且我不知道如何在安卓应用连接后在网络服务中创建会话。
    • 我认为this 链接可能会对您有所帮助。
    • 这个链接是关于安卓应用的。您能否更具体地解释一下 android 应用程序和 Web 服务之间的流程。我认为它们之间的流程:首先是 android 应用程序登录,然后 Web 服务创建 cookie 或会话,并且 android 应用程序可以使用 Web 服务中的其他方法。对吗?
    猜你喜欢
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多