views:

265

answers:

2

I am trying to port my usage meter from a JavaScript Gadget (win) / Widget (OSX) to Android. Total newbie when comes to JAVA + Eclipse + Android 2.1 SDK. Essentially what I want to do is load a page, pass through a username and password and load the resulting page into a array that I can then run some regular expressions through.

My code from JavaScript (I've replace the actual URL with a dummy one) is:

xmlhttp.open("post","https://acme.com.au/your_account/index.php?function=login",false);
xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("check_username=" + username + "&password=" + password);

I need to know the Android equivalent to the above please. I've played around with WebView but that loads the page in a web browser which isn't what I want.

A: 

If you don't want to include a third party library, you could somthing along the lines below:

URL url = new URL("https://acme.com.au/your_account/index.php?function=login");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

// If you need a cookie from previous requests
//conn.setRequestProperty("Cookie", "JSESSIONID=" + encode(jSessionId, "UTF-8")); 
conn.setRequestProperty("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);

DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(String.format("check_username=%s&password=%s",
        URLEncoder.encode(PERSNR, "UTF-8"),
        URLEncoder.encode(PASSWD, "UTF-8")));

out.close();

// I have absolutely no idea why this is needed.
InputStream is = conn.getInputStream();
// ... read the response from is.
is.close();

However I encourage you to use some third party library for anything less trivial than this. I use Apache HttpClient in my Android widget :)

aioobe
Great thanks, I'll give it a try. Would the syntax be simpier if I use the Apache HttpClient?
bebeTech
Sure. Have a look at the [examples](http://hc.apache.org/httpcomponents-client/examples.html). The main benefit as I see it however, is the maturity of the code. I guarantee you that you'll have fewer bugs to fix if you leverage a well tested library to do the task.
aioobe
Great. This example seems to be exactly what I am after: http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java.Just one question though, on Android, where does System.out.println send the output? I've tried it in the past but nothing appears on the device?
bebeTech
Actually figured our the println issue but I am stuck with getting the actual page contents. All I seem to be able to get are status messages like "HTTP/1.1 200 OK". I want to see the contents of the page instead.
bebeTech
I suggest you post a new question since these comments don't really work well with code-formatting and so on. Feel free to drop a link to the follow-up quetion here in the comments though :)
aioobe
I've managed to get it to work. 8-) Was missing a responsehandler. I now have a problem using REGEX which I'll post in a new question
bebeTech
A: 

So I've been using this method for a few days now (http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java) and the app now keeps crashing with a Protocol 443 error - something about unable to resolve the address. Worked fine when first installed and then just stopped. The same with on the device itself, worked fine until this morning and now the same issue?? Nothing wrong with the URL as I can still load in a browser.

bebeTech