【问题标题】:Android app crashing when accessing online mySql database访问在线 mySql 数据库时,Android 应用程序崩溃
【发布时间】:2016-10-02 21:33:26
【问题描述】:

我目前正在尝试直接从我的应用程序访问在线数据库,而没有网络服务...我意识到这有多么愚蠢,但这只是为了项目演示。任何帮助/建议表示赞赏:)

这是我访问数据库的代码:

namespace OmnicalMobile
{
    [Activity(Label = "Omnical",MainLauncher =true, Icon = "@drawable/icon" )]
    public class MainActivity : Activity
    {

        public EditText txtNumber;
        private Button btnYo;
        private EditText txtPassword;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            //ActionBar.Dispose();
            btnYo = FindViewById<Button>(Resource.Id.logBtn);
            txtNumber = FindViewById<EditText>(Resource.Id.editText3);
            txtPassword = FindViewById<EditText>(Resource.Id.editText4);
            btnYo.Click += BtnYo_Click;
            // Get our button from the layout resource,
            // and attach an event to it

        }

        private void BtnYo_Click(object sender, EventArgs e)
        {
            MySqlConnection con = new MySqlConnection("(HIDDEN FROM INTERNET GRIEFERS)");
            MySqlCommand command;
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    Toast.MakeText(Application.Context, "Connection success", ToastLength.Short).Show();

                    MySqlDataReader reader = null;
                    String storedPassword ="";

                    MySqlCommand cmd = new MySqlCommand("SELECT COUNT(*) FROM student WHERE student_number ='" + txtNumber.Text + "'", con);

                    con.Open();
                    int count = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();

                    if (count != 0)
                    {
                        String SQLQuery = "SELECT password FROM student WHERE student_number ='" + txtNumber.Text + "' AND course = 'Information Technology'";
                        command = new MySqlCommand(SQLQuery, con);

                        con.Open();

                        command.ExecuteNonQuery();
                        reader = command.ExecuteReader();

                        while (reader.Read())
                        {
                            storedPassword = (string)reader["password"];
                        }

                        testPassword(txtNumber.Text, txtPassword.Text, storedPassword);

                    }
                    else
                    {
                        Toast.MakeText(Application.Context, "No such student exists", ToastLength.Short).Show();
                        txtPassword.Text = null;
                        txtNumber.Text = null;
                    }
                }

            }
            catch (MySqlException ex)
            {
                Toast.MakeText(Application.Context, ex.Message, ToastLength.Short).Show();

            }
            finally
            {
                con.Close();
            }
        }
        private void testPassword(String userNumber, String password, String storedPassword)
        {
            if (password.Equals(storedPassword))
            {
                string number = txtNumber.Text;
                var second = new Intent(this, typeof(Activity1));
                second.PutExtra("Number", number);
                Finish();
                StartActivity(second);

    }
            else
            {
                Toast.MakeText(Application.Context, "Incorrect Password", ToastLength.Short).Show();
                txtPassword.Text = null;
                txtNumber.Text = null;
            }
        }
    }

以下是我的 logcat 崩溃报告:

10-02 23:16:27.742 I/MonoDroid(16374): UNHANDLED EXCEPTION:
10-02 23:16:27.752 I/MonoDroid(16374): System.NotSupportedException: No data is available for encoding 1252.
10-02 23:16:27.752 I/MonoDroid(16374):   at System.Text.Encoding.GetEncoding (Int32 codepage) <0xeeb7e920 + 0x00648> in <filename unknown>:0 
10-02 23:16:27.752 I/MonoDroid(16374):   at System.Text.Encoding.GetEncoding (System.String name) <0xeeb7ced8 + 0x00033> in <filename unknown>:0 
10-02 23:16:27.752 I/MonoDroid(16374):   at MySql.Data.MySqlClient.Driver..ctor (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) <0xeeb7ccf8 + 0x00023> in <filename unknown>:0 
10-02 23:16:27.752 I/MonoDroid(16374):   at MySql.Data.MySqlClient.Driver.Create (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) <0xeeb7ca18 + 0x00073> in <filename unknown>:0 
10-02 23:16:27.752 I/MonoDroid(16374):   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection () <0xeeb7c9c8 + 0x00013> in <filename unknown>:0 
10-02 23:16:27.752 I/MonoDroid(16374):   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection () <0xeeb7c408 + 0x0022b> in <filename unknown>:0 
10-02 23:16:27.752 I/MonoDroid(16374):   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver () <0xeeb7c1e0 + 0x00057> in <filename unknown>:0 
10-02 23:16:27.752 W/art     (16374): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
10-02 23:16:27.752 D/AndroidRuntime(16374): Shutting down VM
10-02 23:16:27.762 E/AndroidRuntime(16374): FATAL EXCEPTION: main
10-02 23:16:27.762 E/AndroidRuntime(16374): Process: OmnicalMobile.OmnicalMobile, PID: 16374
10-02 23:16:27.762 E/AndroidRuntime(16374): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
10-02 23:16:27.762 E/AndroidRuntime(16374):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
10-02 23:16:27.762 E/AndroidRuntime(16374): Caused by: java.lang.reflect.InvocationTargetException
10-02 23:16:27.762 E/AndroidRuntime(16374):     at java.lang.reflect.Method.invoke(Native Method)
10-02 23:16:27.762 E/AndroidRuntime(16374):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
10-02 23:16:27.762 E/AndroidRuntime(16374):     ... 1 more
10-02 23:16:27.762 E/AndroidRuntime(16374): Caused by: android.runtime.JavaProxyThrowable: System.NotSupportedException: No data is available for encoding 1252.
10-02 23:16:27.762 E/AndroidRuntime(16374):   at System.Text.Encoding.GetEncoding (Int32 codepage) <0xeeb7e920 + 0x00648> in <filename unknown>:0 
10-02 23:16:27.762 E/AndroidRuntime(16374):   at System.Text.Encoding.GetEncoding (System.String name) <0xeeb7ced8 + 0x00033> in <filename unknown>:0 
10-02 23:16:27.762 E/AndroidRuntime(16374):   at MySql.Data.MySqlClient.Driver..ctor (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) <0xeeb7ccf8 + 0x00023> in <filename unknown>:0 
10-02 23:16:27.762 E/AndroidRuntime(16374):   at MySql.Data.MySqlClient.Driver.Create (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) <0xeeb7ca18 + 0x00073> in <filename unknown>:0 
10-02 23:16:27.762 E/AndroidRuntime(16374):   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection () <0xeeb7c9c8 + 0x00013> in <filename unknown>:0 
10-02 23:16:27.762 E/AndroidRuntime(16374):   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection () <0xeeb7c408 + 0x0022b> in <filename unknown>:0 
10-02 23:16:27.762 E/AndroidRuntime(16374):   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver () <0xeeb7c1e0 + 0x00057> in <filename unknown>:0 
10-02 23:16:27.762 E/AndroidRuntime(16374):     at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
10-02 23:16:27.762 E/AndroidRuntime(16374):     at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)
10-02 23:16:27.762 E/AndroidRuntime(16374):     at android.view.View.performClick(View.java:5697)
10-02 23:16:27.762 E/AndroidRuntime(16374):     at android.widget.TextView.performClick(TextView.java:10813)
10-02 23:16:27.762 E/AndroidRuntime(16374):     at android.view.View$PerformClick.run(View.java:22526)
10-02 23:16:27.762 E/AndroidRuntime(16374):     at android.os.Handler.handleCallback(Handler.java:739)
10-02 23:16:27.762 E/AndroidRuntime(16374):     at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 23:16:27.762 E/AndroidRuntime(16374):     at android.os.Looper.loop(Looper.java:158)
10-02 23:16:27.762 E/AndroidRuntime(16374):     at android.app.ActivityThread.main(ActivityThread.java:7224)
10-02 23:16:27.762 E/AndroidRuntime(16374):     ... 3 more
10-02 23:16:27.762 W/ActivityManager( 3452):   Force finishing activity OmnicalMobile.OmnicalMobile/md5d0cf5a481b8ca811cb223058ac8e5810.MainActivity

【问题讨论】:

  • 检查this你有编码问题。
  • @DominiqueLorre 感谢您的帮助!现在一切正常。
  • 哦天啊...您的代码包含 SQL 注入风险!使用准备好的语句而不是通过字符串连接构建命令。 en.wikipedia.org/wiki/SQL_injection

标签: android mysql sql xamarin xamarin.android


【解决方案1】:

使用线程运行这个,你会得到解决方案。(Java和Java android)

new Thread(new Runnable(){
    public void run() {
        your_function();
    }
});

【讨论】:

    猜你喜欢
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 2017-03-26
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多