简体   繁体   中英

Android App: Log-in to website, maintain session - Java

I am new to Android programming and am expecting to developing an app. The app should be able to log-in to a website by filling a HTML form.

Once logged in the session should be maintained and through out the session, the user must be able to post (using standard HTML form posts) to the website.

Is this type of activity possible? Any special modules that I can use for this?

Note: The web-site doesn't have any fancy API. Neither do I have access to its source code.

Thank you in advance.

I wasn't sure of what I was asking at first, after few Bings I landed on what I needed.

The need was to log-in to a web-site by processing a HTML Post form, keep the session and work on the website.

A simple function to connect to a site is as follows.

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();
    }

}

I get a single instance of an HttpClient through an Singleton implementation. And use it when ever I want.

Form attributes are stored as BasicNameVluePair s in a List. An HttpPost object is created, url provided through setURI method. Then the list is provided to the HttpPost object as an entity and finally executed.

Some thing pretty obvious, lot of instructions in StackOverflow and Bing if you need to find.

I guess thats it :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM