为什么MySQL无法连接到Android Studio中的应用程序?

时间:2019-09-21 22:05:50

标签: java mysql 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中显示各种水果及其价格的数据库。

1 个答案:

答案 0 :(得分:3)

Android不支持现成的MySQL。访问数据库的“常规”方法是将一个Restful服务器放在它的前面,并使用HTTPS协议连接到Restful前端。

看看ContentProvider。通常用于访问本地数据库(SQLite),但可以用于从任何数据存储中获取数据。

我确实建议您考虑在本地拥有所有/某些网站数据的本地副本,这样,当Android设备没有连接时,您的应用仍然可以运行。如果沿着这条路线走,那么可以使用一项服务来使两个数据库保持同步。