Open anandakrishnan18 opened 3 years ago
I think your testJython
task is failing, so the server is not started, and you get the "connection refused" message. What do you see running gradlew testJython
?
The failure may be due to (at least) a couple of reasons:
pypackage
section.Thanks for your response. I will try your solutions. Well am I loading the main.py file correctly? Or do I need to use "file:///android_asset/main.py"? Where can I find more documentation about the gradle-jython plugin.
Yes, you are loading the main.py file correctly. The following works for me, meaning the server is started an responds to the browser. I found the dependencies by trial-and-error, looking at "missing module" errors. As I said, only Python v. 2 modules can be used.
build.gradle
plugins {
id "com.github.rzabini.gradle-jython" version "1.1.0"
}
jython {
pypackage 'flask:1.1.4', 'markupsafe:1.1.1', 'jinja2:2.11.3',
'werkzeug:1.0.1', 'itsdangerous:1.1.0', 'click:7.1.2'
}
task testJython(type:jython.JythonTask) {
script file("src/main/assets/main.py")
}
main.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "hello world"
app.run(debug=False, port=8118)
./gradlew testJython
> Task :testJython
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8118/ (Press CTRL+C to quit)
I ran gradlew testJython and it successfully gives the message
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8118/ (Press CTRL+C to quit)
When I search localhost:8118 in my laptop browser, it successfully displays "hello world " flask webpage. But when I build the app, it throws the webview error "net::ERR_CONNECTION_REFUSED". In webview, I use
WebView webView = (WebView) findViewById(R.id.webview); webView.loadUrl("http://localhost:8118");
So flask is running in my laptop, but not in my phone(I use physical device instead of an emulator to test apps). My question is how can I use flask in my android app using webview and gradle-jython
I am not familiar with Android, but I guess you should find the IP address of your laptop as it is seen by your phone, because for your phone localhost
is an address of its own. Your laptop should have an "external" IP, usually assigned by your router (in a local network), something like 192.168..... If your phone is connected to the same LAN, it should be able to reach the server started on the laptop, provided that you find a way for Flask to listen also on that address, in addition to localhost
.
I am new to android development, but not to programming. I am expert with python but I can't manage to use java according to my wishes. So I used the jython gradle plugin. In my main.py, I have the following code:
`from flask import Flask
app = Flask(name)
@app.route("/") def home(): return "hello world"
if name == "main": app.run(debug=False, port=8118)`
Then in my java code, I create a webview and load the url "http://localhost:8118". But it throws out the error, net::ERR_CONNECTION_REFUSED. I thing the jython plugin fails to create the flask app and run it in the android phone and access it in the webview. Well it can be some other problem too. In my build.gradle( Project ):
task testJython(type:jython.JythonTask) { jython{ pypackage 'Flask:2.0.0' } script file("C:/Users/username/AndroidStudioProjects/my-app-name/app/src/main/assets/main.py") }
My main.py is in the assets folder. How to solve this problem?