python - How to send and receive data between flask framework web server and android app -
i unable communicate receive , send reply between web server(which uses flask framework) , android app code shown below. if possible can please post sample server end code might solve problem.
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(); } } } }
the simplest case handling http post request is:
from flask import flask, request app = flask(__name__) @app.route("/data", method=("post",)) def handle_data(): return "hello world - sent me " + str(request.values) if __name__ == '__main__': app.run()
Comments
Post a Comment