当前位置:编程学习 > python >>

100分求助:android程序如何调用python脚本

写了一个android程序,想在android程序中,调用python脚本,这个要怎么调呢?望高手指点

在Java环境下:

import javax.script.*;

import org.python.core.PyFunction;  
import org.python.core.PyInteger;  
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

import java.io.*;  
import static java.lang.System.*;  

public class Start {
public static void main(String[] args)
{
PythonInterpreter interpreter = new PythonInterpreter();  
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");  
interpreter.exec("print days[4];");  

System.out.println(CallPython());
}

private static String CallPython()
{
PythonInterpreter interpreter = new PythonInterpreter();  
        interpreter.execfile("D:\\Java\\Python4Android\\test.py");  
        PyFunction func = (PyFunction)interpreter.get("add",PyFunction.class);  
  
        int a = 2020, b = 2 ;  
        PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));  
        return pyobj.toString();  
}
}



以上代码,是可以正常调用python的,但是将代码移植到android下,在模拟器上运行,就会报错了,android代码如下:

package com.xiejie.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class Start extends Activity {
@Override 
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String text = CallPython();

TextView t = (TextView)findViewById(R.id.textView1);
t.setText(text);
}

private String CallPython()
{
PythonInterpreter interpreter = new PythonInterpreter();  
        interpreter.execfile("/mnt/sdcard/test.py");  
        PyFunction func = (PyFunction)interpreter.get("add",PyFunction.class);  
  
        int a = 2020, b = 2 ;  
        PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));  
        return pyobj.toString();  
}
}

--------------------编程问答-------------------- 自己先顶一个 --------------------编程问答-------------------- --------------------编程问答-------------------- 請問您有找到原因嗎?
我也想要Java內崁Python 執行Java function/class --------------------编程问答-------------------- --------------------编程问答-------------------- 试过kivy吗? https://github.com/kivy/python-for-android --------------------编程问答-------------------- 楼主,该问题搞定了吗? --------------------编程问答-------------------- 楼主兄问题解决了吗?我也在弄这个... --------------------编程问答-------------------- 请教过程,我也在搞这个,谢谢! --------------------编程问答-------------------- 是不是要安装 Python的 APK 才行
http://www.cnblogs.com/hicjiajia/archive/2012/02/05/2339130.html --------------------编程问答-------------------- 楼主解决了吗。关注。。。
补充:移动开发 ,  Android
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,