-5

我是 Android 应用程序的新手。开发,我正在尝试开发一个应用程序。可以通过 jtds 达到 ms sql 2008 r2。

我已经使用 sqllite 数据库来创建连接字符串。

我收到android os.NetworkOnMainThreadException错误,请帮助我,我的代码如下,

private void giris() {

    DB db = new DB(this);
    db.open();
    Cursor c = db.Query();
    String ipAdresi = null, Port = null, veriTabaniAdi = null, kullaniciAdi = null, sifre = null;

    while (c.moveToNext()) {
        ipAdresi = c.getString(c.getColumnIndex("IpAdresi"));
        Port = c.getString(c.getColumnIndex("PORT"));
        kullaniciAdi = c.getString(c.getColumnIndex("KullaniciAdi"));
        sifre = c.getString(c.getColumnIndex("Sifre"));
    }

    url = "jdbc:jtds:sqlserver://" + ipAdresi + ":" + Port + ";databaseName=";
    driver = "net.sourceforge.jtds.jdbc.Driver";
    userName = kullaniciAdi;
    password = sifre;
    db.close();
    ResultSet results = null;
    try {
        EditText txtTest = (EditText)findViewById(R.id.editTextip);
        EditText txtName = (EditText)findViewById(R.id.editTextport);
        String user = txtTest.getText().toString();
        String pass = txtName.getText().toString();

        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(url, userName, password);
        Statement statement = conn.createStatement();
        results = statement.executeQuery("Select * From Kullanıcı where KULLANICI = '" + user + "' and SIFRE = '"+ pass + "'");

        if(!results.next())
        {
            Toast.makeText(this, "Hoşgeldiniz ", Toast.LENGTH_SHORT).show();
            try {
                Class d = Class.forName("com.example.endustrinetbistro.Girismenu");
                Intent intent = new Intent(this, d);
                startActivity(intent);
            } catch (ClassNotFoundException e) {
                Toast.makeText(this, "Hata : " + e.toString(),
                        Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
          do{
                Toast.makeText(this, "Hata : " + "Kullanıcı Adınız, Şifreniz veya Baglantı Ayarlarınızda Bir Sorun var ", Toast.LENGTH_SHORT)
                .show();
          }while(results.next());
        }
    } catch (Exception e) {
        Toast.makeText(this, "Hata : " + e.toString(), Toast.LENGTH_SHORT)
                .show();
    }


}

}
4

1 回答 1

0

始终使用 asynctask 来包装数据库调用或网络调用。这些调用可能需要很长时间才能处理,如果从主线程运行,将有可能阻塞应用程序 UI。在异步任务中,您在 doInBackground() 方法中所做的任何事情都将在后台线程中运行。

于 2013-04-04T03:23:13.733 回答