Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!