I get this exception and I don’t know why, that’s the classes I have:
An activity which starts a service which starts a class which is a ScheduledExecutorService that connect to the db(that’s why the connection thing).
When I tried to connect through my service I understood why I got this exception but now I created a new class/task to do that but I’m still getting this exception.
I didn’t attached code since it’s alot of code and the problem might be in the way I managed this system and not in the code itself, if you would like to see anything else let me know.
See Managing a connection to the mysql through a service for more information on why a Thread isn’t appropriate in this case.
EDIT
code attachment:
this is my third class that sends details to the 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();
}
}
}
EDIT
I changed the task into a thread here is the new code:
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();
}
}
}
How come now that I’m getting the NetworkOnMainThread exception??
The exception means that you are performing a long term operation on the UI thread, like a network operation eg. Since android 3, those operations are not allowed and the exception is thrown. So, in order to avoid the exception you should execute your code in a async way. You do not have to create class, but you have to create a class that extend
ThreadorAsyncTask.