我是Android應用程序新手。開發和我正在嘗試開發一個應用程序。可以通過jtds到達ms sql 2008 r2。如何修復NetworkOnMainThreadException?
我已經使用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();
}
}
}
使用'AsyncTask'或其他構造來完成主(UI)線程的網絡操作。或者,關閉嚴格模式。 – Tushar 2013-04-04 03:17:56
你在哪裏叫giris()?它不應該從主線程調用。 – sandrstar 2013-04-04 03:20:10
只需搜索一下。這比輸入一個問題更省力 - 更好的是,你將學會如何自己做! – 323go 2013-04-04 03:21:31