HttpURLConnection空响应Android和Python Flask

时间:2014-04-25 19:21:50

标签: android google-app-engine flask httpurlconnection

如果我忘了一些规则,首先发帖请原谅我:P

我正在开发一款Android应用,需要将基本的来回数据传输到谷歌应用引擎上的应用。该应用程序是用python编写的,我正在使用flask框架与应用程序进行通信(通过http请求,不安全以及我知道的其他所有内容,但我只想要一个概念证明,而不是其他任何东西)。

我的问题是,当python应用程序部署在gae上并且我从物理设备上的android应用程序发送请求时,我没有得到任何响应,但是如果我在浏览器中放入必要的URL,我会收到响应否问题。

无论如何java函数代码:

    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;
}

和python函数:

@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"

任何帮助都将非常感谢!另外python代码工作,复制/粘贴操作有点搞砸了。

更新 我弄清楚出了什么问题。我错过了一些难题的基本部分,但感谢@hgoebl指出我的错误(即整个功能,我的自我)。所以这里是可能需要它的任何人的替换代码。

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();
        } 
    }

还要将这几行放在onCreate()中:

StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

0 个答案:

没有答案