question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simple Example Android API Code..

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
hushanhsiangcommented, Jan 30, 2018

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:

from java import *


class py_class():
    def __init__(self, name):
        print( name)
    @property
    def py_return1(self):
        return("Return form py_return-1")

    @classmethod
    def py_return2(self):
        return("Return form py_return-2")

    def py_return3(self):
        return("Return form py_return-3")

Java side:

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");

        PyObject stream =py_test.callAttr("py_class", "Oscar").get("py_return1");
        System.out.println(stream.toString());

        stream = py_test.get("py_class").callAttr("py_return2");
        System.out.println(stream.toString());

        stream = py_test.callAttr("py_class", "Chaquopy").callAttr("py_return3");
        System.out.println(stream.toString());

    }
}

There’s no any problem and the Logcat result:

01-30 08:03:34.014 4400-4400/com.oscarhu.python.java_call_py1 I/python.stdout: Oscar
01-30 08:03:34.014 4400-4400/com.oscarhu.python.java_call_py1 I/python.stdout:  
01-30 08:03:34.016 4400-4400/com.oscarhu.python.java_call_py1 I/System.out: Return form py_return-1
01-30 08:03:34.017 4400-4400/com.oscarhu.python.java_call_py1 I/System.out: Return form py_return-2
01-30 08:03:34.018 4400-4400/com.oscarhu.python.java_call_py1 I/python.stdout: Chaquopy
01-30 08:03:34.019 4400-4400/com.oscarhu.python.java_call_py1 I/python.stdout:  
01-30 08:03:34.020 4400-4400/com.oscarhu.python.java_call_py1 I/System.out: Return form py_return-3
01-30 08:03:34.037 4400-4642/com.oscarhu.python.java_call_py1 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

This simple example of Java API will be no problem with simple argument transmission. Thank you very much.

1reaction
hushanhsiangcommented, Jan 25, 2018

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

package com.oscarhu.python.java_call_py1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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.py_print");
        PyObject stream= py_test.call("Chaquopy");
        System.out.println(stream);

I need a way for Java call Python’s method and function with return a string. Thanks.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found