android - 如何在flask framework web server和android app之间发送和接收数据

标签 android python flask

我无法在 web 服务器(使用 flask 框架)和我的 android 应用程序之间进行通信以接收和发送回复,其代码如下所示。 如果可能的话,请发布一个示例服务器端代码,这可能会解决我的问题。

package com.project.nsj;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    String userName, passkey;
    EditText username, password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        Button login = (Button) findViewById(R.id.login_button);

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //checkLogin login = new checkLogin();
                //login.execute();
                Intent funcs = new Intent(MainActivity.this, Functions.class);
                startActivity(funcs);
            }
        });

    }

    public class checkLogin extends AsyncTask<Void, Void, Integer> {

        @Override
        protected Integer doInBackground(Void... params) {

            userName = username.getText().toString();
            passkey = password.getText().toString();

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://rpihomeautomation.no-ip.biz");

            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
            nameValuePair.add(new BasicNameValuePair("username", userName));
            nameValuePair.add(new BasicNameValuePair("passkey", passkey));

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            } catch (UnsupportedEncodingException e) {

                e.printStackTrace();
            }

            HttpResponse response;
            int status = 0;
            try {
                response = httpClient.execute(httpPost);
                status = response.getStatusLine().getStatusCode();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return status;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (result == 200) {
                Intent funcs = new Intent(MainActivity.this, Functions.class);
                startActivity(funcs);
            } else {
                Toast.makeText(MainActivity.this, "Invalid password/username",
                        Toast.LENGTH_LONG).show();
            }

        }
    }

}

最佳答案

处理 HTTP POST 请求的最简单情况是:

from flask import Flask, request

app = Flask(__name__)

@app.route("/data", method=("POST",))
def handle_data():
    return "Hello World - you sent me " + str(request.values)

if __name__ == '__main__':
    app.run()

关于android - 如何在flask framework web server和android app之间发送和接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21595558/

相关文章:

python - 如何在Linux上搭建Python开发环境?

python - MongoEngine 改变数据库

python - 将我的 'next' url 存储在签名的 cookie 中并无忧无虑地重定向到它是否安全?

php - 限制对我服务器上的 php 脚本的访问

android - 来自异步任务的上下文和回调

python - Convert_obj_two.py 语法错误

python - 如何使用flask将内容加载到div元素中

javascript - 将数据从 Jinja 传回 Flask

java - Android 按钮故障

Android Studio 在 UI 上显示乱码