过去几天我一直试图解决我的Android代码问题,我执行两个不同的线程,第一个在我启动应用程序时执行的工作完美且没有错误查询。 但是,单击提交按钮时执行的第二个线程不执行SQL查询。
以下是代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submitButton = (Button) findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this,"This works",Toast.LENGTH_LONG).show();
service = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
checkPermission();
criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
if (provider != null & !provider.equals("")) {
Location location = service.getLastKnownLocation(provider);
service.requestLocationUpdates(provider, 2000, 1, MainActivity.this);
if (location != null) {
onLocationChanged(location);
} else {
Toast.makeText(MainActivity.this, "location not found", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this, "Provider is null", Toast.LENGTH_LONG).show();
}
selectionEvent = eventSpinner.getSelectedItem().toString();
insertThread.start();
}
});
sqlThread.start();
}
Thread sqlThread = new Thread(){
public void run(){
alertHandler = new Handler(Looper.getMainLooper());
try{
Class.forName("org.postgresql.Driver");
String url = "localhost:5432";
Connection conn = DriverManager.getConnection(url,"arodriguez","alejandro2017");
String statementSQL = "SELECT * FROM incidencias";
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(statementSQL);
while(resultSet.next()){
tiposIncidencias.add(resultSet.getString(2));
naturalezaIncidencias.add(resultSet.getString(3));
}
alertHandler.post(new Runnable() {
@Override
public void run() {
addDefaultItemsOnTypeSpinner();
addDefaultItemsOnEventSpinner();
}
});
String statementSQL2 = "INSERT INTO arodriguez.prueba (description,x,y) VALUES ('"+selectionEvent+"',"+latitude+","+longitude+")";
Statement s = null;
s = conn.createStatement();
s.executeUpdate(statementSQL2);
conn.close();
} catch (SQLException se){
Toast.makeText(MainActivity.this,se.toString(),Toast.LENGTH_LONG).show();
} catch (ClassNotFoundException e){
Toast.makeText(MainActivity.this,e.toString(),Toast.LENGTH_LONG).show();
} catch (Exception ex){
Log.d("Error",ex.toString());
}
return;
}
};
Thread insertThread = new Thread(){
public void run(){
alertHandler = new Handler(Looper.getMainLooper());
try{
alertHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,selectionEvent,Toast.LENGTH_LONG).show();
}
});
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/sde";
Connection conn = DriverManager.getConnection(url,"arodriguez","alejandro2017");
String statementSQL = "INSERT INTO arodriguez.prueba (description,x,y) VALUES ('"+selectionEvent+"',"+latitude+","+longitude+")";
Statement statement = null;
statement = conn.createStatement();
statement.executeUpdate(statementSQL);
conn.close();
}catch (SQLException se){
Log.d("Error",se.toString());
} catch (ClassNotFoundException e){
Log.d("Error",e.toString());
} catch (Exception ex){
Log.d("Error",ex.toString());
}
return;
}
};
出于某种原因,如果我在第一个线程中添加插入查询,它确实有效:
String statementSQL = "INSERT INTO arodriguez.prueba (description,x,y) VALUES ('"+selectionEvent+"',"+latitude+","+longitude+")";
Statement statement = null;
statement = conn.createStatement();
statement.executeUpdate(statementSQL);
我做错了吗?