[英]Android App: Log-in to website, maintain session - Java
I am new to Android programming and am expecting to developing an app. 我是Android编程的新手,并希望开发一个应用程序。 The app should be able to log-in to a website by filling a HTML form. 该应用程序应该能够通过填写HTML表单登录到网站。
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. 登录会话后应予以维护,并在整个会话期间,用户必须能够(使用标准HTML表单发布)发布到网站。
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. 注意:该网站没有任何精美的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. 一开始我不确定我要问的是什么,在几次Bings之后我就找到了我需要的东西。
The need was to log-in to a web-site by processing a HTML Post form, keep the session and work on the website. 需要通过处理HTML Post表单登录到网站,保持会话状态并在网站上工作。
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. 我通过Singleton实现获得HttpClient的单个实例。 And use it when ever I want. 并在需要时使用它。
Form attributes are stored as BasicNameVluePair s in a List. 表单属性以BasicNameVluePair的形式存储在List中。 An HttpPost object is created, url provided through setURI method. 创建一个HttpPost对象,通过setURI方法提供url。 Then the list is provided to the HttpPost object as an entity and finally executed. 然后,该列表作为一个实体提供给HttpPost对象,并最终执行。
Some thing pretty obvious, lot of instructions in StackOverflow and Bing if you need to find. 有些事情很明显,如果需要查找,可以在StackOverflow和Bing中找到很多说明。
I guess thats it :) 我想就是这样:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.