1
I want to connect my android app direct with the bank that is on a server. I made the connection in an Asynctask, follows the code.
public class ConnectionTask extends AsyncTask<Object, Object, Connection> {
private final Context context;
private final String endereco = "jdbc:postgres://192.168.1.36/sics";
private final String usuario = "postgres";
private final String senha = "postdba";
private ProgressDialog progress;
public ConnectionTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(context, "Aguarde...",
"Tentando realizar conexão com o banco de dados!", true, true);
}
@Override
protected Connection doInBackground(Object... params) {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver").newInstance();
conn = DriverManager.getConnection(endereco, usuario, senha);
} catch (Exception e) {
Log.i("CONEXAO", "NAO CONECTADO " + e.getMessage());
}
return conn;
}
@Override
protected void onPostExecute(Connection result) {
progress.dismiss();
}
}
So far everything is without error, but error in my DAO. Follow my method list below.
public List<Categoria> listarCategoria(Context context) {
List<Categoria> lista = new ArrayList<Categoria>();
/*Cursor c = helper.getReadableDatabase().rawQuery(
"select * from categorias", null);
while (c.moveToNext()) {
Categoria categoria = fill(c);
lista.add(categoria);
}
return lista;*/
Connection conn = new ConnectionTask(context).execute();
PreparedStatement stmt = conn.prepareStatement("select * form categoria");
return lista;
}
You’re making a mistake on the line
Connection conn = new ConnectionTask(context).execute();
Follow a print with the error
Thank you very much in advance! :)
I think there’s a conceptual problem. Your
ConnectionAsyncTask
is an abstraction to perform a background processing (doInBackground
) in aThread
separate, publish progress in theMainThread
(onProgressUpdate
) and finish with the UI update (onPostExecute
). Just when it runs theexecute
of hisAsyncTask
, is starting this process, and not blocking theThread
current waiting for the result. To fix the problem you need to notify this class, which calls theAsyncTask
, within the methodonPostExecute
with the received parameter.– Wakim
How do I do it?
– meisterx7
Actually the very idea of the mobile phone connecting directly to the remote bank is a conceptual problem. Instead, it should call a web service (REST for example) from a server that connects to the database. There’s a lot of material about it on the net.
– Piovezan
The point is, this is my college project, and time is very, very short. We were going to make a web service, but due to the time we decided to connect directly with the bank and if we have time we will make a WS.
– meisterx7
My reply (http://answall.com/a/34073/6436) exemplifies two ways of notifying the UI from another
Thread
or Class, both solutions are valid in your case, choose the one you find best. There are other ways, but I’ve limited it to just two so it doesn’t get too long.– Wakim
You see, friend, that last code, the IP address that Voce used is the local IP address of the computer where the bank is?
– Jeferson Lemos