Android Studio:接口错误:2003:无法连接到MySQL服务器上'192.168.137.1:3306'

ogsagwnx  于 2023-05-05  发布在  Android
关注(0)|答案(1)|浏览(208)

我对这项技术很陌生。但目前我正在Android上做一个项目,其中有一个集成模块,我使用Python代码,因为我需要实现ML从MySQL数据库中获取一些数据。
在应用ML之前,我只是尝试在与我的项目集成的Android应用程序中使用Python脚本获取数据。
对于MySQL,我在PC上安装了XAMPP服务器。
每当我尝试执行该应用程序,它总是崩溃的错误。

2023-05-02 12:57:57.277 23850-23850/com.example.chaquopy E/USNET: USNET: appName: com.example.chaquopy
2023-05-02 12:57:59.426 23850-23850/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.chaquopy, PID: 23850
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chaquopy/com.example.chaquopy.MainActivity}: com.chaquo.python.PyException: InterfaceError: 2003: Can't connect to MySQL server on '192.168.137.1:3306' (1 Operation not permitted)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4035)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4201)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2438)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8663)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
     Caused by: com.chaquo.python.PyException: InterfaceError: 2003: Can't connect to MySQL server on '192.168.137.1:3306' (1 Operation not permitted)
        at <python>.mysql.connector.network.open_connection(network.py:602)
        at <python>.mysql.connector.connection._open_connection(connection.py:570)
        at <python>.mysql.connector.abstracts.connect(abstracts.py:1181)
        at <python>.mysql.connector.connection.__init__(connection.py:164)
        at <python>.mysql.connector.pooling.connect(pooling.py:294)
        at <python>.myScript.<module>(myScript.py:13)
        at <python>.importlib._bootstrap._call_with_frames_removed(<frozen importlib._bootstrap>:219)
        at <python>.importlib._bootstrap_external.exec_module(<frozen importlib._bootstrap_external>:843)
        at <python>.java.android.importer.exec_module(importer.py:554)
        at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:671)
        at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:975)
        at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
        at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
        at <python>.importlib.import_module(__init__.py:127)
        at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModuleNative(chaquopy_java.pyx:129)
        at com.chaquo.python.Python.getModuleNative(Native Method)
        at com.chaquo.python.Python.getModule(Python.java:84)
        at com.example.chaquopy.MainActivity.onCreate(MainActivity.java:31)
        at android.app.Activity.performCreate(Activity.java:8290)
        at android.app.Activity.performCreate(Activity.java:8270)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4009)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4201) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2438) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:226) 
        at android.os.Looper.loop(Looper.java:313) 
        at android.app.ActivityThread.main(ActivityThread.java:8663) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) 

下面是我的代码:

MainActivity.java
package com.example.chaquopy;

import androidx.appcompat.app.AppCompatActivity;

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

import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;

public class MainActivity extends AppCompatActivity {

        TextView textview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview=findViewById(R.id.tv1);

        if (! Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }//will start python

        //create python instance
        Python py=Python.getInstance();

        //create python object
        PyObject pyobj=py.getModule("myScript");//giving python script name

        //return text in TextView
        textview.setText(py.toString());
    }
}
myScript.py
import mysql.connector as mysql

# enter your server IP address/domain name
HOST = "192.168.137.1" # or "domain.com"

# database name, if you want just to connect to MySQL server, leave it empty
DATABASE = "recipe"
# this is the user you create
USER = "test"
# user password
PASSWORD = "123456"
# connect to MySQL server
connection = mysql.connect(host=HOST, port="3306", database=DATABASE, user=USER, password=PASSWORD)

cursor = connection.cursor()
# some other statements  with the help of cursor
retrive = "Select ingredients from final_merge;"

#executing the quires
cursor.execute(retrive)
rows = cursor.fetchall()
for row in rows:
    print(row)

#commiting the connection then closing it.
connection.commit()
connection.close()

这里192.168.137.1是移动的电话的网关IP,该移动电话连接到我笔记本电脑上的热点。
同样的Python脚本在我的笔记本电脑上PyCharm中运行时运行良好,但当我试图在移动的上运行它时,它崩溃了。
我需要一种方法,我应该能够与我的PC上的数据库连接,并运行此代码。

inkz8wg9

inkz8wg91#

手机和笔记本上的Python版本是一样的吗?电话是否运行防火墙?您的电话上显示的TCPIP地址是什么?您可以通过电话ping mySQL Server IP吗?你有没有试过通过VPN从手机连接到mySQL盒子,看看是否能解决这个问题?
https://android.stackexchange.com/questions/46499/how-configure-the-dhcp-settings-of-wifi-tethering-hotspot-on-android
你也可以考虑安装一些类似Termux的东西

相关问题