Simple Example Android API Code..
See original GitHub issueI’d like to write a simple example code of Android API but failed. Please give me some ideas.
The python code which is in “py_test.py” of “test1” python package folder :
from java import *
class py_print():
def __init__(self, name):
name="Chaquopy"
print(name)
The Java code:
import com.chaquo.python.*;
public class MainActivity extends AppCompatActivity {
protected Python py;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
py = Python.getInstance();
PyObject py_test = py.getModule("test1.py_test");
stream= py_test.py_print.call("Chaquopy");
System.out.println(stream)
Plan to send a string to python from Java, and python return a string to Java. python can print the string from Java and Java can printh the string from python, to confirm if it works. Thanks.
Issue Analytics
- State:
- Created 6 years ago
- Comments:17 (9 by maintainers)
Top Results From Across the Web
Samples - Android Developers
A selection of code samples and templates for you to use to accelerate your app development. Browse samples to learn how to build...
Read more >Android From Scratch: Using REST APIs - Code Tutsplus
In this tutorial, I'm going to show you how to use the classes and methods available in the Android SDK to connect to...
Read more >How to Build Your First Android App (Using an API) - Rapid Blog
How to Build an Android App with an API · Step 1: Start a new Kotlin project within the Android Studio. · Step...
Read more >How to use a web API from your Android app
Learn how to use a web API from within an Android app using Retrofit. Get posts from social media, store data on the...
Read more >How To Consume Data From an API in Android - Section.io
Step 1 - Getting started · Step 2 - Installing dependencies · Step 3 - Reviewing the Movie API · Step 4 -...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s great to have your words. I modify my code to the three condition as below: Python code which is in “py_test.py” of “test1” python package folder:
Java side:
There’s no any problem and the Logcat result:
This simple example of Java API will be no problem with simple argument transmission. Thank you very much.
Ok, I knew above code will generate Grandle error, previously, I need your correction to let it work.
Is there a document to descript how Java can call the method of Python’s class or Python’s function? And I need a simple example.
Below modified code without Grandle error but Logcat generate:
No module named 'test1.py_test.py_print'; 'test1.py_test' is not a package
I need a way for Java call Python’s method and function with return a string. Thanks.