【问题标题】:doInBackground() is not working when using Async with JDBC将 Async 与 JDBC 一起使用时,doInBackground() 不起作用
【发布时间】:2019-11-26 12:38:08
【问题描述】:

我正在尝试从托管在服务器上的数据库 (MySQL) 中获取一些数据。 为此,我将 JDBC 与 Async 结合使用。我在另一个应用程序中使用了相同的代码。而且效果很好。但是当我试图在这个应用程序中做同样的事情时。未调用 doInBackGround() 方法。

我已将连接类用作内部类。并初始化了一个名为 last 的数组。我已将其第 0 个索引更改为某个随机字符串并在 postExecute() 中打印出来。但它是空的,我是怎么知道 doInBackground() 从来没有被调用过的。任何帮助,将不胜感激。谢谢

    public class connection extends AsyncTask<String, String, String> {
        Context context;
        String[] last = new String[5];


        connection(Context ctx) {
            context = ctx;

        }

        @Override
        protected String doInBackground(String... strings) {
            last[0] = "something";
//            try {
//                Class.forName("com.mysql.jdbc.Driver");
//                Connection con = DriverManager.getConnection(DB_URL, USER, PASS);
//
//                if (con == null) {
//                        last[0]= "con issue";
//                } else {
//                    String Query = "select * from users where ID like '" + search + "';";
//                    last[0] = "in else";
//                    Statement stmt = con.createStatement();
//                    last[0] = "after stm";
//                    ResultSet res = stmt.executeQuery(Query);
//                    last[0] = "after execute";
//                    while (res.next()) {
//                        last[0] = "insidewhile";
////                        last[0] =  res.getString("NAME");
////                        last[1] = res.getString("KISAM");
////                        last[2] = res.getString("MOKA");
////                        last[3] = res.getString("ONWAN");
////
//                        return null;
//                    }
//
//                    last[0] = "not";
//                }
//                con.close();
//            } catch (ClassNotFoundException e) {
//                last[0] = e.getException().getMessage();
//                Log.e("ClassNotFoundException", e.getException().getMessage());
//                e.printStackTrace();
//            } catch (SQLException e) {
//                last[0] = e.toString();
//                Log.e("SQL ERROR",e.toString());
//                e.printStackTrace();
//            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            Toast.makeText(Search.this, "Searching", Toast.LENGTH_SHORT).show();

        }

        @Override
        protected void onPostExecute(String s) {
            String S = last[0];
            if(TextUtils.isEmpty(S) || s == null){
                Toast.makeText(context, " doinBackground() not called", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(Search.this, "S", Toast.LENGTH_LONG).show();
//                if (!S.contentEquals("not")) {
//                    kisa = (last[1]);
//                    mok = (last[2]);
//                    Name = (last[0]);
//                    onwa = (last[3]);
//                    AF.setText(Name);
//                    AF.setVisibility(View.VISIBLE);
//                    pb.setVisibility(View.GONE);
//                } else {
//
//                    Toast.makeText(Search.this, "NO Record Found", Toast.LENGTH_SHORT).show();
//                }
            }


        }

        @Override
        protected void onProgressUpdate(String... values) {

        }

    }

logcat 中没有错误。甚至我也登录了 catch 块。

【问题讨论】:

标签: mysql jdbc


【解决方案1】:

Android 新手在这里。我认为最好制作一个 PHP 脚本来完成所有这些工作,并通过 HttpURLConnection 调用它。

您还应该使用 PreparedStatements 而不是 Statements 以避免 SQL 注入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2017-11-05
    • 2015-03-01
    • 2019-04-15
    • 2014-06-19
    • 2018-01-14
    相关资源
    最近更新 更多