• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Converting a python script to java for android

exutable

Lurker
Hello,

I wrote a python script to pull my telmore "money(danish cellular provider), I was wondering what processes I should use to convert this to Java, for use in an android app. What libraries etc?

Please also comment if I have written something ineffectively, wrong practice etc.

Code is of course released under GPLv2:

Code:
import cookielib
import hashlib
from BeautifulSoup import BeautifulSoup

#Accept cookies to browse telmore website
cookieJar = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))

#Add headers
opener.addheaders = [('User-agent', "Mozilla/5.0")]

#Accept arguments for username and password
username = sys.argv[1]
password = sys.argv[2]

#Url requests, first page is login, second is a frame of the login page
url = "https://www.telmore.dk/t2/j_security_check"
url2 = "https://www.telmore.dk/t2/mytelmore/index.do"

#Fill the forms with the username and password arguments
form = { "j_username" : username,
         "j_password" : password }

#Encode the form and create request		 
encodedForm = urllib.urlencode(form)
request = urllib2.Request(url, encodedForm)
page = opener.open(request)

#Request the second page
request = urllib2.Request(url2)
page = opener.open(request)
contents = page.read()

#Beatiful soup to parse the html and sort out the span tag with class saldo
soup = BeautifulSoup(contents)
saldospan = soup.findAll('span')
saldo = saldospan[0].contents[0].strip()
print saldo

Here is my progress so far, but for some reason it doesn't print anything

Code:
import java.net.*;
import java.io.*;

public class TelmoreSaldo {
    public static void main(String[] args) throws Exception {
	try {
        // Construct data
        String data = URLEncoder.encode("j_username", "********") + "=" + URLEncoder.encode("j_password", "**********");
    
        // Send data
        URL url = new URL("https://telmore.dk/t2/j_security_check");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
    
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
        wr.close();
        rd.close();
    } catch (Exception e) {
    }
}

I already tried posting this on Ubuntu forums, but I don't think it's really the right place as I have barely gotten any replies.

Converting a python script to java for android - Ubuntu Forums
 
Back
Top Bottom