从Java中的Android应用执行Python脚本? [英] Execute Python script from Android app in Java?

查看:65
本文介绍了从Java中的Android应用执行Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种从Android中的Java代码执行Python脚本的方法.我对此事进行了研究,但发现的唯一发现是,如何在Android版APK(Kivy e.t.c.)中转换python脚本.

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.).

更具体地说,我有一个包含很多功能的脚本.我要做的就是在我的Java代码中基于此python脚本创建一个对象,并通过该对象来调用我的函数.

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.

我无法将Python代码转换为Java,因为我使用了仅存在于python中的各种库.

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.

以下是我的Python脚本的摘录.我使用库密码进行加密.

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)

推荐答案

一个选项是创建一个Web服务API,以允许通过HTTP提供您的python结果.然后,这只是Android与HTTP Web服务进行通信的问题,这很简单.

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.

您可能会发现一些python到Java的桥梁,但以我的经验,这些东西在实践中总是有局限性,理论上似乎很棒,但最终无法正常工作.

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.

这篇关于从Java中的Android应用执行Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆