繁体   English   中英

为什么 MySQL 没有连接到我在 Android Studio 中的应用程序?

[英]Why is MySQL not connecting to my app in Android Studio?

我正在处理这个家庭作业并尝试连接到本地 MySQL 数据库,该应用程序安装并在我的 AVD 中运行,但是当我单击“获取数据”按钮时,我收到了 connError 消息。

我对此很陌生,想进入这个领域,所以我查阅了多个视频和文章,但由于某种原因,我无法找到解决这个问题的方法。 这是我的主要 java 代码。

public class MainActivity extends AppCompatActivity {

    ItemAdapter itemAdapter;
    Context thisContext;
    ListView myListView;
    TextView progressTextView;
    Map<String, Double> fruitsMap = new LinkedHashMap<String, Double>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Resources res = getResources();
        myListView = findViewById(R.id.myListView);
        progressTextView = findViewById(R.id.progressTextView);
        thisContext = this;

        progressTextView.setText("");
        Button btn = findViewById(R.id.getDataBtn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GetData retrieveData = new GetData();
                retrieveData.execute("");
            }
        });
    }

    private class GetData extends AsyncTask<String, String, String> {

        String msg = "";
        // JDBC driver name and database URL
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        // Example: 10.20.30.40.3306
        static final String DB_URL = "jdbc:mysql://localhost:3306/fruits" +
                DbStrings.DATABASE_URL + "/" +
                DbStrings.DATABASE_NAME;

        @Override
        protected void onPreExecute() {
            progressTextView.setText("Connecting to database...");
        }

        @Override
        protected String doInBackground(String... params) {

            Connection conn = null;
            Statement stmt = null;

            try {
                Class.forName(JDBC_DRIVER);
                conn = DriverManager.getConnection
                        (DB_URL, DbStrings.USERNAME, DbStrings.PASSWORD);

                stmt = conn.createStatement();
                String sql = "SELECT * FROM fruits";
                ResultSet rs = stmt.executeQuery(sql);

                while (rs.next()) {
                    String name = rs.getString("item");
                    double price = rs.getDouble("price");

                    fruitsMap.put(name, price);
                }

                msg = "Process complete";

                rs.close();
                stmt.close();
                conn.close();


            } catch (SQLException connError) {
                msg = "An exception was thrown for JDBC.";
                connError.printStackTrace();
            } catch (ClassNotFoundException e) {
                msg = "A class not found exception was thrown.";
                e.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected  void  onPostExecute(String msg) {
            progressTextView.setText(this.msg);

            if (fruitsMap.size() > 0) {
                itemAdapter = new ItemAdapter(thisContext, fruitsMap);
                myListView.setAdapter(itemAdapter);
            }
        }
    }

} //End of MainActivity

预期结果应该是在 TextView 中显示各种水果及其价格的数据库。

Android 不支持开箱即用的 MySQL。 访问数据库的“正常”方式是在其前面放置一个 Restful 服务器并使用 HTTPS 协议连接到 Restful 前端。

看看内容提供者。 它通常用于访问本地数据库 (SQLite),但也可用于从任何数据存储中获取数据。

我确实建议您在本地查看所有/部分网站数据的本地副本,这样当 Android 设备没有连接时,您的应用程序仍然可以工作。 如果您沿着这条路线走 go,那么可以使用服务来保持两个数据库同步。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM