Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Converting a python script to java for android

  1. #1
    Join Date
    Sep 2008
    Beans
    178
    Distro
    Ubuntu 9.10 Karmic Koala

    Converting a python script to java for android

    Hello,

    I wrote a python script to pull my telmore usage(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 urllib
    import urllib2
    import sys
    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

  2. #2
    Join Date
    Oct 2005
    Beans
    66
    Distro
    Ubuntu Karmic Koala (testing)

    Re: Converting a python script to java for android

    Out of interest, have you tried running this under the Android Scripting Environment (available from the Market?)

    As an alternative a free program in the Market to do the same is NetCounter.

  3. #3
    Join Date
    Sep 2008
    Beans
    178
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Converting a python script to java for android

    The scripting environment wasn't in the market, I'll look for it in a little from external sources.

    The script doesn't watch net usage, it grabs the amount of money you have left on the account.

  4. #4
    Join Date
    Oct 2005
    Beans
    66
    Distro
    Ubuntu Karmic Koala (testing)

    Re: Converting a python script to java for android

    Sorry it's at http://code.google.com/p/android-scripting/

    'it grabs the amount of money you have left on the account'
    Great feature!

  5. #5
    Join Date
    Sep 2008
    Beans
    178
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Converting a python script to java for android

    Thanks buddy,

    this looks really good.

  6. #6
    Join Date
    Sep 2008
    Beans
    178
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Converting a python script to java for android

    So even though I got ASE working, I kind of want to turn this into a real app. So here is my code so far but it isn't returning anything and I'm pretty sure I'm encoding everything right.

    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) {
        }
    }
    }

  7. #7
    Join Date
    Sep 2008
    Beans
    178
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Converting a python script to java for android

    bump?

  8. #8
    Join Date
    Oct 2005
    Beans
    66
    Distro
    Ubuntu Karmic Koala (testing)

    Re: Converting a python script to java for android

    Sorry, Java's not a strong point of mine. You might get more luck asking on one of the Android Developer forums.

    Did it work OK under the ASE btw? I've found the ASE to be pretty good.

    Regards

  9. #9
    Join Date
    Sep 2008
    Beans
    178
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Converting a python script to java for android

    Oh yeah totally worked, it was awesome getting Python running.

    Yeah I just posted a thread on androidforums.org

    http://androidforums.com/android-dev...a-android.html

  10. #10
    Join Date
    Jan 2009
    Location
    Maryland
    Beans
    10

    Re: Converting a python script to java for android

    In the catch block, try sticking a e.printStackTrace() to see if theres an exception thats ending it early...

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •