java NetworkOnMainThread异常
我遇到这个异常,我不知道为什么,这就是我的课程: 一个活动,它启动一个服务,该服务启动一个类,该类是一个ScheduledExecutorService,连接到db(这就是连接的原因)
当我尝试通过我的服务连接时,我理解了为什么会出现这种异常,但现在我创建了一个新类/任务来实现这一点,但我仍然会遇到这种异常
我没有附加代码,因为它有很多代码,问题可能是我管理这个系统的方式,而不是代码本身,如果你想看到其他任何东西,请告诉我
有关Thread
在这种情况下不合适的原因的更多信息,请参见Managing a connection to the mysql through a service
编辑 代码附件: 这是我的第三个类,它向db发送详细信息
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异常
# 1 楼答案
你可以通过下面的指南来解决你的问题
蜂巢(3.0或更高版本)会出现此错误
您不能像文档所说的那样在其主线程上执行联网操作若要使用此功能,必须使用handler或asynctask。恐怕没有别的办法了
你可以看到更多细节WHY ICS Crashes your App
尝试使用下面的代码片段
# 2 楼答案
异常意味着您正在UI线程上执行一个长期操作,比如网络操作。自从android 3以来,这些操作是不允许的,并且会引发异常。因此,为了避免异常,您应该以异步方式执行代码。您不必创建类,但必须创建一个扩展
Thread
或AsyncTask
的类