Execute Python script from Android app in Java? - java

I am trying to find a way to execute a Python script from my Java code in Android. I did a research on this matter but the only thing I found is, how I can convert python scripts in APK for android (Kivy e.t.c.).
More specifically I have a script which contains a lot of functions.What i want to do is to create an object based on this python script inside my java code and via this object to call my functions.
I can't convert my Python code into Java, because I use various libraries that exists only in python.
Any advice would be helpful and deeply appreciated.
The following a snippet of my Python script. I use the library charm for cryptography.
from charm.core.math.integer import integer,serialize,deserialize
class serializeClass:
def __init__(self)
...
def serialize(self, charm_object):
assert type(charm_object) == integer, "required type is integer, not: ", type(charm_object)
return serialize(charm_object)
def deserialize(self, object):
assert type(object) == bytes, "required type is bytes, not: ", type(object)
return deserialize(object)

One option would be to create a Web Service API to allow your python results be served up over HTTP. Then it's just a matter of Android communicating with an HTTP web service which is simple.
You might find some python to java bridge out there but in my experience these things always have limitations in practice and seem great in theory but don't end up working correctly.

You can use SL4A project which would allow you run your Python code on Android.
Github: https://github.com/damonkohler/sl4a
Tutorials: https://github.com/damonkohler/sl4a/blob/wiki/Tutorials.md

Check out Qpython, could be an easy solution. You can run your python script against the Qpython android python core, or deploy yours.
Checkout this repository: https://github.com/qpython-android/app-call-qpython-api

Related

Call java function from python - Chaquopy

I went through Chaquopy docs and found; with Chaquopy, java and python code can be called as user wishes.
I went through example apps and found examples for either calling python from java or running Android code through python and not the other way like calling java function from python.
Is there some example available or some reference for calling java function from python?
It would be better to add such example on Github examples if not already available.
Referred Chaquopy sample app on Github
The API reference is here, and there is a large example here.
If you have a specific problem, please ask another question and explain exactly what you're trying to do.

Connecting to GoogleRefine using Java program

This question is similar to the posting 'Script-driven automation of Google refine with ruby python perl java or otherwise': Script-driven automation of Google refine with ruby python perl java or otherwise
I have a lengthy JSON Script that I created in GoogleRefine and I am using it to clean several individual text files. Currently, I need to load each file manually into GoogleRefine and run the JSON script.
Is there a Java code/library that I can use to connect to GoogleRefine and pass on my JSON script and text file locations in order to speedily clean all of my files? I don't know Python and since the post above is dated 2011, I am wondering if a Java libray/program has been written since then.
Today there is no Java library. You can find them in following languages:
python
ruby
JavaScript
You can read more about the OpenRefine API here: https://github.com/OpenRefine/refine-python/wiki/Refine-API

How to execute java functions/script in PHP

I have a system which is implemented using PHP(Joomla). Now the client wants to integrate a SMS gateway. Unfortunately SMS gateway does not supoort PHP. It's written in Java and All the examples are written in JAVA.
I'm supposed to include webservices‐rt.jar to my program and run a example script as below.
lk.mobitel.esms.User user = new lk.mobitel.esms.User (); user.setUsername(“TestUser”);
user.setPassword(“Password”);
lk.mobitel.esms.test.ServiceTest st = new lk.mobitel.esms.test.ServiceTest ();
System.out.println(st.testService(u));
According to my knowledge I can run a .jar as below using PHP
<?php exec("java -jar filename.jar arguments",$output); ?>
How can I run a JAVA script like above in PHP? Is it possible? Is there a way to bridge PHP and JAVA? What I want is to run JAVA in PHP else I would say communicate between these two.
I never did this, but PHP Manual gave a link for PHP-Java Bridge for PHP5 as PHP/Java Bridge. If it is what you are looking for ?
It's not possible to combine PHP and Java in the same script and have them executed in one run.
Your exec approach is the way to go, if you are approaching from PHP. But you should rather use passthru as exec does not return anything. With passthru you can get the shell response of your java command and act accordingly.
Is there a way to bridge PHP and JAVA? What I want is to run JAVA in PHP else I would say communicate between these two.
Simple answer:
Bridge using data-exchange layer. :)
Hint (in super simple language): My favorite webservice uses PERL to process their scripts and I fetch them using XML.
Groupon is made on RoR but, I happily get tweets, thanks to their API which outputs a JSON.
Simple? :)

Android: Call Python Script (via SL4A) from Java code

Is it possible to call/run a python script file from Java code on the Android platform using SL4A? Basically I have a full blown Java Android app and I have several Python scripts that scrape some information from various web pages. I would like to be able to call these python scripts with the web page and get the results back. Is this possible? If so, can anyone point me in the direction of an example or two?
Thank you,
Harry
run an SL4A script from my application?
http://code.google.com/p/android-scripting/wiki/FAQ#Can_I_run_an_SL4A_script_from_my_application?
Here you go, a solution on how to make Android applications using SL4A and Python. I have developed a functional Python application following this tutorial so I can vouch for it. I assume you can add the Python code and do your data exchange with JSON.
I personally have never tried to mix these two together, though I will probably try after seeing your post.
Also, if you have any problems/questions, related to starting that example, post them here, I might help.

how to import a .Jar file in Java Script

I want to import a .Jar file in Java Script,
there is a Class in the Jar file which i want to use in Java script,and here is a code i found but couldn't get it work yet !
var cl = new Packages.java.net.URLClassLoader(
[
new Packages.java.net.URL('http','My Ip', '8080', 'Hello.jar')
]
);
var aClass = Packages.java.lang.Class.forName("PackageAddress.Hello", true, cl);// Hello is a public class name
var aStaticMethod = aClass.getMethod("SayHello", []);//SayHello is a public method returning a string
var greeting = aStaticMethod.invoke(null, []);
alert(greeting);
Any Suggestions would be appreciated
You don't.
JavaScript is not Java.
And trying to use Java code within your JavaScript ExtJS code will a complete overkill, requiring users to have Java plugin installed in their browsers, and slowing down their browsers because of Java plugin. Of course, supposing it might work (and I'm not sure about that).
You want to call webservices from your JavaScript code? (Re)Write the webservice-calling in JavaScript. Not only this is the best solution, but I'm pretty sure it will require less work than trying to set up a Java applet and trying to interact with it.
There's another way to think of this. If you are trying to indeed write a JavaScript application with a Java backend, you probably would struggle to get all the support you need from the JavaScript community. But if you think of it as a Java application with a JavaScript frontend, all of a sudden, you have lots of options. I'm sure SpringBoot alone could give you many complete solutions. Here's one for React.js.
Furthermore, feel free to look at some of these new frameworks I found at Raygun.com. I haven't had the opportunity to use any of these myself but I believe one in particular seems to have the make up of what you're looking for. It was listed as Vaadin.

Categories

Resources