views:

254

answers:

3

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.

A: 

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.

WarrenFaith
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.
Alejandro Huerta
A: 

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.

brambury
This looks like what I am looking for. I will look into it more, thank you.
Alejandro Huerta
+1  A: 

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.

unhillbilly