HttpURLConnection Empty response Android and Python Flask

1 view
Skip to first unread message

onzlo via StackOverflow

unread,
Apr 25, 2014, 3:27:15 PM4/25/14
to google-appengin...@googlegroups.com

First post so forgive me if I forgot some rules :P

I'm developing an android app that requires basic to and fro data transfer to an app on google app engine. That app is written in python and I'm using the flask framework to communicate with the app (through http request, insecure and everything else I know but I just want a proof of concept more than anything else at the moment).

My problem is that when the python app is deployed on gae and I send a request from the android app on a physical device, I get no response, yet if I put the necessary URL in the browser, I get a response no problem.

Anyway the java function code:

    private boolean doLogin(String username, String passwd){
    boolean loggedIn = false;
    try {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        URL url = new URL("http://appname.appspot.com/login/admin/admin");// + username + "/" + passwd+"/");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        //con.setRequestMethod("POST");
        InputStream in = new BufferedInputStream(con.getInputStream());

        if (readStream(in) == true){
            loggedIn = true;
        }
        //con.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }


    return loggedIn;
}

And the python function:

@webapp.route('/login/<username>/<passwd>', methods=["GET", "POST"])
def dologin(username, passwd):

def validusertype(u2check, p2check):

    if db.GqlQuery("SELECT * FROM UserAccount WHERE username = :u2check  AND password = :passwd", u2check = u2check, passwd=p2check).count() > 0:
        qry = db.GqlQuery("SELECT * FROM UserAccount WHERE username = :u2check", u2check = u2check)

        for r in qry.fetch(limit=None):
            return r.usertype

    return None

if request.method == "GET":
    _type = validusertype(username, passwd)
    if _type:
        return "True"
# elif request.method == "POST":


# Fall though - if nothing above takes, we end up here.
return "False"

Any help at all would be greatly appreciated!



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/23301317/httpurlconnection-empty-response-android-and-python-flask

onzlo via StackOverflow

unread,
Apr 25, 2014, 4:52:19 PM4/25/14
to google-appengin...@googlegroups.com

Any help at all would be greatly appreciated! Also the python code works, Copy/paste operation kinda screwed it up a bit.

Update I figured out what was wrong. I was missing a few essential pieces to the puzzle but thanks to @hgoebl for pointing out my errors (ie the whole function, ow my ego). So here's the replacement code for anyone that may need it.

try {
        URL url = new URL("http://app.appspot.com/login/admin/admin");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();

        is = conn.getInputStream();

        // Convert the InputStream into a string
        String contentAsString = readStream(is);
        return contentAsString;

    // Makes sure that the InputStream is closed after the app is
    // finished using it.
    } finally {
        if (is != null) {
            is.close();
        } 
    }

Also to put these few lines in onCreate():

StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Reply all
Reply to author
Forward
0 new messages