Java android.os.on主线程异常
我正在尝试使用以下代码连接到sql server上的sql DB。 但是当我运行它时,上面的错误显示出来。 我已将权限行添加到清单中,但仍然没有成功 任何建议都将不胜感激Java android.os.on主线程异常,java,android,sql,Java,Android,Sql,我正在尝试使用以下代码连接到sql server上的sql DB。 但是当我运行它时,上面的错误显示出来。 我已将权限行添加到清单中,但仍然没有成功 任何建议都将不胜感激 Log.i("Android"," MySQL Connect Example."); Connection conn = null; try { String driver = "net.sourceforge.jtds.jdbc.Driver"; Class.forName(driver).newInsta
Log.i("Android"," MySQL Connect Example.");
Connection conn = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
//test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
String connString = "jdbc:jtds:sqlserver://server_ip_address :1433/DBNAME;encrypt=fasle;user=xxxxxxxxx;password=xxxxxxxx;instance=SQLEXPRESS;";
String username = "xxxxxx";
String password = "xxxxxxxxxx";
conn = DriverManager.getConnection(connString,username,password);
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("select * from TableName");
//Print the data to the console
while(reset.next()){
Log.w("Data:",reset.getString(3));
//Log.w("Data",reset.getString(2));
}
conn.close();
} catch (Exception e) {
Log.w("Error connection","" + e.getMessage());
}
NetworkOnMainThreadException:当应用程序尝试在其主线程上执行网络操作时引发的异常
您应该在asynctask上调用sendfeedback方法,这样只有上面的代码才能工作。由于Web服务器需要花费大量时间来响应,主线程变得无响应。为了避免它,你应该在另一个线程上调用它。因此更好
为什么我已经有-2了?原因是什么?因为你甚至没有在谷歌上搜索一次异常。不管怎么说,问题是你们在主线程上访问网络?你们可以在TimerTask中创建定时器并进行网络操作