Converting a python script to java for android


Last Updated:

  1. exutable

    exutable Member This Topic's Starter

    Joined:
    Sep 5, 2009
    Messages:
    8
    Likes Received:
    0
    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 (Text):
    1. import cookielib
    2. import hashlib
    3. from BeautifulSoup import BeautifulSoup
    4.  
    5. #Accept cookies to browse telmore website
    6. cookieJar = cookielib.LWPCookieJar()
    7. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))
    8.  
    9. #Add headers
    10. opener.addheaders = [('User-agent', "Mozilla/5.0")]
    11.  
    12. #Accept arguments for username and password
    13. username = sys.argv[1]
    14. password = sys.argv[2]
    15.  
    16. #Url requests, first page is login, second is a frame of the login page
    17. url = "https://www.telmore.dk/t2/j_security_check"
    18. url2 = "https://www.telmore.dk/t2/mytelmore/index.do"
    19.  
    20. #Fill the forms with the username and password arguments
    21. form = { "j_username" : username,
    22.          "j_password" : password }
    23.  
    24. #Encode the form and create request      
    25. encodedForm = urllib.urlencode(form)
    26. request = urllib2.Request(url, encodedForm)
    27. page = opener.open(request)
    28.  
    29. #Request the second page
    30. request = urllib2.Request(url2)
    31. page = opener.open(request)
    32. contents = page.read()
    33.  
    34. #Beatiful soup to parse the html and sort out the span tag with class saldo
    35. soup = BeautifulSoup(contents)
    36. saldospan = soup.findAll('span')
    37. saldo = saldospan[0].contents[0].strip()
    38. print saldo
    39.  
    Here is my progress so far, but for some reason it doesn't print anything

    Code (Text):
    1. import java.net.*;
    2. import java.io.*;
    3.  
    4. public class TelmoreSaldo {
    5.     public static void main(String[] args) throws Exception {
    6.     try {
    7.         // Construct data
    8.         String data = URLEncoder.encode("j_username", "********") + "=" + URLEncoder.encode("j_password", "**********");
    9.    
    10.         // Send data
    11.         URL url = new URL("https://telmore.dk/t2/j_security_check");
    12.         URLConnection conn = url.openConnection();
    13.         conn.setDoOutput(true);
    14.         OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    15.         wr.write(data);
    16.         wr.flush();
    17.    
    18.         // Get the response
    19.         BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    20.         String line;
    21.         while ((line = rd.readLine()) != null) {
    22.             System.out.println(line);
    23.         }
    24.         wr.close();
    25.         rd.close();
    26.     } catch (Exception e) {
    27.     }
    28. }
    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
     

    Advertisement

Share This Page

Loading...