简体   繁体   中英

Android app crashing when accessing online mySql database

I'm currently trying to access an online database directly from my app to the database with no web-service... I realize how dumb this is but its just for a project presentation. Any help/advice is appreciated :)

Here is my code that's accessing the database:

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;
            }
        }
    }

Below are my logcat reports from the crash:

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

Run this using thread, you will get the solution.(Java and Java android)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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