0

I'm attempting to turn a web interface into an Android one by parsing the web page for relevant data and then allowing the user to send back data to the server. Using simple HttpClient code I have managed to get the web page and then parse the necessary information. I'm unfamiliar with sending data back, so, I'm hoping someone can shed some light. Here's the relevant HTML code from the login page.

<table cellspacing=0 cellpadding=0><tr><td valign=top align=center>


<table cellspacing=0 cellpadding=0 border=0  width=220 align=center class=table_back><tr><td>
<table cellspacing=1 cellpadding=1 border=0 width=100%>
<tr class=table_row1><form action="i.cfm?&1028&p=login&se=4" method=post name=stepform><Td align=right nowrap>&nbsp;Empire Name&nbsp;</td><td>&nbsp;<input type=text name=nic size=16 ></td></tr>
<tr class=table_row2><Td align=right>Password&nbsp;</td><td>&nbsp;<input type=password name=password size=16 ></td></tr>

<tr class=table_row1><Td align=right valign=top>Server</td><td>&nbsp;<select name=server>


<option value="0" >Normal</option>

<option value="1" >Fast</option>

<option value="2" >Slow</option>

<option value="3" >Ultra</option>

<option value="4" selected>RT</option>


</select><font class=smallfont> <a href=javascript:ch('i.cfm?popup=help&type=server');>What is this <img src=i/help.gif></a>
</td></tr>
<tr class=table_row2><Td align=right>&nbsp;IP&nbsp;</td><td>&nbsp;69.47.105.149 <font class=smallfont>(United States of America)</font></td></tr>
<tr class=table_row1><td>&nbsp;</td><td>&nbsp;<input type=submit value="  Login  " ></td></tr>

</td></tr></table></table>

As you can see there are 3 inputs needed, the "Empire Name", "Password", and the "Server" which consist of 5 options. How would I go about sending this data back to the server over httpClient, assuming that I have gathered the relevant information form my Android GUI. Any help is greatly appreciated.

5
  • You're parsing HTML to generate the UI for your application? That seems like a terrible idea. What are you actually trying to accomplish?
    – noah
    Aug 23, 2010 at 18:48
  • Seems like scraping web page for form inputs and presenting them as Android GUI controls to the user. Interesting actually...
    – Mchl
    Aug 23, 2010 at 18:56
  • Exactly what Mchl said, I'm scraping the web page for relevant information. On this particular page I scrape for the IP address that the site shows in, <tr class=table_row2><Td align=right>&nbsp;IP&nbsp;</td><td>&nbsp;69.47.105.149 <font class=smallfont>(United States of America)</font></td></tr> I then present it in a more UI friendly format in the GUI. But the only issue is sending back the information that I gather from the User within my GUI back to the server in a way the the original website would. Aug 23, 2010 at 19:28
  • And what are you going to do when the website changes and your app is completely broken?
    – noah
    Aug 23, 2010 at 19:53
  • I'll deal with that when/if that time comes. In the mean time, constructive comments are more than welcome ;) Aug 23, 2010 at 20:32

3 Answers 3

1

If you're going to do more than scrape one page and/or want a development tool to help you, take a look at Web Harvest. It may have dependencies outside of what Android provides, but it is under BSD license should you have to adapt it to the target platform. Source is here.

0

The code may look like the following:

private void postDataToServer() throws UnsupportedEncodingException, IOException {

    /* 
     * Taken from "action" field in the form. This can be absolute address
     * so take this into account.
     */
    String action = "i.cfm?&1028&p=login&se=4"; 
    /* This the server you want to send info. */
    String yourServer = "http://your.server.com/";

    /* This form uses "post" method. */
    HttpPost post = new HttpPost(yourServer + action);
    /* Form parameters. */
    List<NameValuePair> params = new ArrayList<NameValuePair>();

    /* This is what the user has entered in the corresponding fields. */
    String nic = getNic();
    String password = getPassword();
    String server = getServer();

    params.add(new BasicNameValuePair("nic", nic));
    params.add(new BasicNameValuePair("password", password));
    params.add(new BasicNameValuePair("server", server));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");

    HttpClient client = new DefaultHttpClient();

    post.setEntity(entity);
    client.execute(post);
}

Maybe this page also may help.

1
  • This looks like what I am looking for. I will look into it more, thank you. Aug 24, 2010 at 3:09
0

I know a application which contains only a webview to display the mobile website. Sounds like thats what you should do, if you want to minimize the native android programming.

1
  • Not really what I'm looking for, if you read my comment in the original question, I want to get rid of the websites original layout and design and redesign in within my Android application so that I can present it in a more UI friendly manner. Aug 23, 2010 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.