Android App:登录网站,维护会话 - Java

Ziy*_*een 2 java session android web

我是Android编程的新手,我期待开发一个应用程序.应用程序应该能够通过填写HTML表单登录到网站.

一旦登录会话应该被维护,并且在整个会话期间,用户必须能够(使用标准HTML表单帖子)发布到网站.

这种活动可能吗?我可以使用任何特殊模块吗?

注意:该网站没有任何花哨的API.我也无法访问其源代码.

先感谢您.

Ziy*_*een 7

我不知道我刚开始问的是什么,经过几次Bings我找到了我需要的东西.

需要通过处理HTML Post表单登录到Web站点,保持会话并在网站上工作.

连接到站点的简单功能如下.

public static void connectToSite(HttpClient client, String username, String password){
    List<NameValuePair> arguments = new ArrayList<NameValuePair>();
    arguments.add(new BasicNameValuePair("email", username));
    arguments.add(new BasicNameValuePair("pword", password));
    arguments.add(new BasicNameValuePair("action", "modifyPALS"));
    arguments.add(new BasicNameValuePair("Submit", "Login"));

    HttpPost post = new HttpPost();

    try{
        post.setURI(new URI("http://www.my-target-website.com/login.php"));
        post.setEntity(new UrlEncodedFormEntity(arguments));
        client.execute(post);
        AppStatus s = getSiteConnectionStatus(client, site);
    }
    catch (URISyntaxException e){
        Log.e("LANKAFRIENDS", "SiteConnection.connectToSite():URISyntaxException");
    } 
    catch (ClientProtocolException e) {
        e.printStackTrace();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }

}
Run Code Online (Sandbox Code Playgroud)

我通过Singleton实现获得了一个HttpClient的单个实例.并在我想要的时候使用它.

表单属性在List中存储为BasicNameVluePair.创建一个HttpPost对象,通过setURI方法提供url.然后该列表作为实体提供给HttpPost对象并最终执行.

有些事情非常明显,如果你需要找到的话,StackOverflow和Bing中有很多指令.

我猜那就是:)