如何在flask框架web服务器和android应用程序之间发送和接收数据

时间:2014-02-06 06:23:28

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

        }
    }

}

1 个答案:

答案 0 :(得分:3)

处理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()