我得到了这个异常,我不知道为什么,这是我拥有的类:一个启动服务的活动,它启动一个类,这个类是一个连接到db的ScheduledExecutorService (这就是为什么连接的原因)。
当我试图通过我的服务连接时,我理解了为什么我会得到这个异常,但是现在我创建了一个新的类/任务来实现这个异常,但是我仍然得到了这个异常。
我没有附加代码,因为它有很多代码,问题可能在于我管理这个系统的方式,而不是代码本身,如果您想看到其他的东西,请告诉我。
有关为什么在本例中不适合使用通过服务管理到mysql的连接的更多信息,请参见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异常??
发布于 2013-01-09 13:28:40
异常意味着您正在UI线程上执行长期操作,如网络操作(如网络操作)。因为android 3,这些操作是不允许的,并且会抛出异常。因此,为了避免异常,您应该以异步方式执行代码。您不必创建类,但必须创建扩展Thread或AsyncTask的类。
发布于 2013-01-09 13:30:57
你可以通过下面的指南来解决你的问题。
android.os.NetworkOnMainThreadException
此错误随HoneyComb(3.0或更高版本)而来。
--您不能像文档所说的那样在其主线程上执行网络操作。必须使用处理程序或异步任务才能完成此任务。AFAIK没有其他方法可以做到这一点。
有关更多细节,您可以看到这个为什么ICS会破坏你的应用程序
试着使用下面的代码片段
new Thread(){
public void run(){
//do your Code Here
}
}.start();https://stackoverflow.com/questions/14236288
复制相似问题