ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • flask서버를 이용한 안드로이드앱 전송
    카테고리 없음 2023. 4. 3. 19:54

     

    import flask
    
    app = flask.Flask(__name__)
    
    @app.route('/', methods=['GET', 'POST'])
    def handle_request():
        postBody = flask.request.get_data()
        postBodyStr = postBody.decode('utf-8')
        print("Post Body:", postBodyStr)
    
        return "Flask Server & Android are Working Successfully"
    
    if __name__ == '__main__':
        app.run(host="192.168.168.163", port=5000, debug=True)
        implementation 'com.squareup.okhttp3:okhttp:3.4.1'
    public class MainActivity extends Activity  {
        private Context context;
        Button btn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            context = this;
    
            btn = findViewById(R.id.connect_btn);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    connectServer();
                }
            });
    
        }
    
        void connectServer(){
            String postUrl = "http://" + "192.168.168.163" + ":" + 5000 + "/";
    
            String postBodyText="안녕하십니까?";
            MediaType mediaType = MediaType.parse("text/plain");
            RequestBody postBody = RequestBody.create(mediaType, postBodyText);
    
            postRequest(postUrl, postBody);
        }
    
        void postRequest(String postUrl, RequestBody postBody) {
    
            OkHttpClient client = new OkHttpClient();
    
            Request request = new Request.Builder()
                    .url(postUrl)
                    .post(postBody)
                    .build();
    
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    // Cancel the post on failure.
                    call.cancel();
    
                    // In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            TextView responseText = findViewById(R.id.responseText);
                            responseText.setText("Failed to Connect to Server");
                        }
                    });
                }
    
                @Override
                public void onResponse(Call call, final Response response) throws IOException {
                    // In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            TextView responseText = findViewById(R.id.responseText);
                            try {
                                responseText.setText(response.body().string());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            });
        }
    }
Designed by Tistory.