mardi 4 août 2015

Send Post Request on Android

Dear StackOverflow community,

Before I start, I would like to say that I am aware of the fact that there are many similar questions to the one I am about to ask, and I have read them and tried answers that could potentially solve my problem. So far, I am unable to find a proper solution.

I am attempting to create an Android application to directly show me my work schedule, without having to log in to the website. Later on, I will add functionality to add the schedule to a Google Calendar, but I'll figure out how when I get the essentials working.

As I have more experience with Java than with Android, I decided to test the basic idea using JavaFX; I created a simple application that logs in, downloads the webpage containing tables with the dates and times, and display them in a neat table of my own. I created this application with the use of HtmlUnit, and got it to work just fine. Unfortunately, it is not possible to use HtmlUnit with Android, and I'm here looking for an alternative.

The basic functionality that I have to acchieve:

  • Load a webpage (http://ift.tt/1OJhDLW) containing the login form
  • Fill in a username and password, and submit the form
  • Get the webpage presented to me, and retrieve data from it
  • Put the information in a HashMap, and use it in a table of my own

The code I used to achieve the wanted result in Java:

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
final WebClient webClient = new WebClient(BrowserVersion.CHROME);

final HtmlPage page1 = webClient.getPage("---HIDDEN---");
final HtmlForm form = page1.getFirstByXPath("//form[@action='/pkmslogin.form']");

final HtmlSubmitInput button = form.getInputByValue("Aanmelden");
final HtmlTextInput usernameField = form.getInputByName("username");
final HtmlPasswordInput passwordField = form.getInputByName("password");

usernameField.setValueAttribute("---HIDDEN---");
passwordField.setValueAttribute("---HIDDEN---");

final HtmlPage page2 = button.click();

final HtmlTable table = page2.getHtmlElementById("viewPC_7_O49HSGU21834502RCI9ODP2022_:tableEx1");

I have yet to find something that will allow me to replicate this on Android. I believe the issue lies within how the web server handles the login, as it does not use a PHP Script. Login Form:

<FORM onSubmit="return checkrequired(this)" METHOD=POST ACTION="/pkmslogin.form">
<FONT SIZE="+2">
<TABLE BORDER="0" WIDTH="400">
<TR>
<TD ALIGN="LEFT"><UL><LI>Gebruikersnaam</LI></UL></TD>
<TD><INPUT TYPE="TEXT" NAME="username" SIZE="15" AUTOCOMPLETE="off">
</TD>
</TR>
<TR>
<TD ALIGN="LEFT"><UL><LI>Wachtwoord</LI></UL></TD>
<TD><INPUT TYPE="PASSWORD" NAME="password" SIZE="15" AUTOCOMPLETE="off"></TD>
<INPUT TYPE="HIDDEN" NAME="login-form-type" VALUE="pwd">
</TR>
</TABLE>
</FONT>
<BR>&nbsp <INPUT TYPE="SUBMIT" VALUE="Aanmelden">
</FORM>

Is anyone willing to provide me with a suitable alternative that will allow me to read the page presented to me after logging in? It will be much appreciated!

Aucun commentaire:

Enregistrer un commentaire