Prem2868 / Mycomputer-

0 stars 0 forks source link

RemoteAndroidAccess #2

Open Prem2868 opened 2 months ago

Prem2868 commented 2 months ago

Import necessary modules from Flask

from flask import Flask, request, jsonify

Initialize a Flask application

app = Flask(name)

Define a route for handling GET and POST requests

@app.route('/api/data', methods=['GET', 'POST']) def handle_data():

Check if the request method is POST

if request.method == 'POST':
    # Get JSON data from the POST request
    data = request.json
    # Handle POST request (e.g., store data, process it)
    return jsonify({'message': 'Data received', 'data': data})

# Check if the request method is GET
elif request.method == 'GET':
    # Handle GET request (e.g., fetch data)
    return jsonify({'message': 'GET request received'})

Run the Flask server on port 5000 and make it accessible from other devices

if name == 'main': app.run(host='0.0.0.0', port=5000)

Prem2868 commented 2 months ago

// Import necessary OkHttp libraries import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response;

import java.io.IOException;

// Create an OkHttpClient instance OkHttpClient client = new OkHttpClient();

// Create a request to communicate with the Python server Request request = new Request.Builder() .url("http://:5000/api/data") // Replace with the server IP address .build();

// Execute the request and handle the response try (Response response = client.newCall(request).execute()) { // Check if the response is successful if (response.isSuccessful()) { // Handle successful response (e.g., parse data, update UI) } else { // Handle error response (e.g., log error, display message) } } catch (IOException e) { // Handle IOException (e.g., network error) }