我收到此异常,我不知道为什么,这就是我所拥有的类(class): 启动一个服务的 Activity ,该服务启动一个连接到数据库的 ScheduledExecutorService 类(这就是连接的原因)。
当我尝试通过我的服务进行连接时,我明白为什么会出现此异常,但现在我创建了一个新的类/任务来执行此操作,但我仍然遇到此异常。
我没有附加代码,因为它有很多代码,问题可能出在我管理这个系统的方式上,而不是代码本身,如果您想查看其他内容,请告诉我。
<小时/>参见Managing a connection to the mysql through a service了解有关为什么 Thread
在这种情况下不合适的更多信息。
编辑 代码附件: 这是我的第三个类,它将详细信息发送到数据库。
public class SendLocation
{
private String lastAddress;
private int id;
private Connection conn;
private PreparedStatement stmt;
private LocationService ls1;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public SendLocation(int id,LocationService ls1)
{
this.lastAddress = "";
this.id = id;
this.conn = null;
this.sqlConnect();
this.sqlInsertStatement();
this.ls1 = ls1;
}
public void send()
{
final Runnable sender = new Runnable()
{
public void run()
{
if(!lastAddress.equals(ls1.getAddress()) && !ls1.getAddress().equals(""))
{
lastAddress = ls1.getAddress();
try
{
stmt.setInt(1, id);
stmt.setString(2, lastAddress);
stmt.execute();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
};
final ScheduledFuture sendHandle =
scheduler.scheduleAtFixedRate(sender, 1, 1, TimeUnit.SECONDS);
scheduler.schedule(new Runnable()
{
public void run()
{
sendHandle.cancel(true);
}
}, 60 * 60, TimeUnit.SECONDS);
}
public boolean sqlConnect()
{
try {
Class.forName(Config.DRIVER).newInstance();
DriverManager.setLoginTimeout(100);
this.conn = DriverManager.getConnection(Config.URL+Config.DBNAME,
Config.USERNAME,Config.PASSWORD);
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void sqlInsertStatement()
{
try {
this.stmt = conn.prepareStatement
("INSERT INTO locations(id, location) VALUES (?,?)");
}catch (SQLException e1) {
e1.printStackTrace();
}
}
}
编辑
我将任务更改为线程,这是新代码:
public class SendLocation extends Thread
{
private String lastAddress;
private int id;
private Connection conn;
private PreparedStatement stmt;
private LocationService ls1;
private boolean run;
public SendLocation(int id,LocationService ls1)
{
this.lastAddress = "";
this.id = id;
this.conn = null;
this.sqlConnect();
this.sqlInsertStatement();
this.ls1 = ls1;
this.run = true;
}
public void run()
{
while(run)
{
if(!lastAddress.equals(ls1.getAddress()) && !ls1.getAddress().equals(""))
{
lastAddress = ls1.getAddress();
try
{
stmt.setInt(1, id);
stmt.setString(2, lastAddress);
stmt.execute();
sleep(1000);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void destroy()
{
this.run = false;
}
public boolean sqlConnect()
{
try {
Class.forName(Config.DRIVER).newInstance();
DriverManager.setLoginTimeout(100);
this.conn = DriverManager.getConnection(Config.URL+Config.DBNAME,
Config.USERNAME,Config.PASSWORD);
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void sqlInsertStatement()
{
try {
this.stmt = conn.prepareStatement
("INSERT INTO locations(id, location) VALUES (?,?)");
}catch (SQLException e1) {
e1.printStackTrace();
}
}
}
为什么现在我收到 NetworkOnMainThread 异常?
最佳答案
该异常意味着您正在 UI 线程上执行长期操作,例如网络操作。从android 3开始,这些操作是不允许的,并且会抛出异常。因此,为了避免异常,您应该以异步方式执行代码。您不必创建类,但必须创建一个扩展 Thread
或 AsyncTask
的类。
关于java - 网络主线程异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14236288/