Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

273
Vistas
Make java call to python and show response

I'm a bit at a loss, I have a python application running on a raspberry pi, and a java application running on another device.

I want to make a call from the java app and have python do some stuff with the request and then send a response back to the java application.

At the moment I can make a call to python and process that, I'm having some issues with sending a response back and have java process it.

For java I have a tomcat server and a flask server for python

For my java code I did the following

public void doSwitch(String urlString) {
    URL url = null;
    HttpURLConnection con = null;
    try {
        url = new URL(urlString);
        con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Accept", "application/json");
        con.getInputStream();
    } catch (IOException e) {
        log.error(String.format("An error occurred: %s", e));
        throw new ConnectionFailedException("An error occurred", e);
    } finally {
        if (con != null) {
            con.disconnect();
        }
    }
}

for python I did the following

import serial
import time
from flask import Flask, redirect, jsonify, url_for

app = Flask(__name__)

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



@app.route('/light/on')
def lights_on():
    serialComs(b"on\n")
    line = serialComs(b"status\n")
    return redirect('/' + line)


@app.route("/light/off")
def lights_off():
    serialComs(b"off\n")
    line = serialComs(b"status\n")
    return redirect('/' + line)


@app.route("/light/status")
def light_status():
    status = serialComs(b"status\n")
    if status == "":
        status = "unknown"
    return status


@app.route("/<lightstatus>")
def lights(lightstatus):
    print(lightstatus)
    return jsonify({'status': lightstatus})


def serialComs(state):
    ser = serial.Serial('/dev/ttyAMA0',9600 , timeout=1)
    ser.flush()
    ser.write(state)
    time.sleep(0.5)
    line = ser.readline().decode('utf-8').rstrip()
    return line

I know that the java code doesn't cater for a response yet, but I'm not sure how to do it yet.

What do I need to look at/read through to have java get some data from python? I have no idea where to look.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It may be problem with CORS. You have to enable to redirect response to other api. In default it is enable to connect via browser only.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda