How to make connections to a php work in the background on Android

Asked

Viewed 379 times

1

I have a little app of chat who logs in and sends messages to the server through php + JSON, but each query crashes the app for a half second, which is really boring.

I heard something called Backgroundworker, but I don’t know exactly how it works.

This is my code:

    EditText campoLogin = (EditText)findViewById(R.id.campo_de_login);
    EditText campoSenha = (EditText)findViewById(R.id.campo_de_senha);
    Conectar Conn = new Conectar("http://meusite.com/chat/login.php?login=" + campoLogin.getText() + "&senha=" + campoSenha.getText() );

    JSONArray array = new JSONArray(Conn.response);
    JSONObject object = array.getJSONObject(0);
    id = (String) object.get("id");
    nome = (String) object.get("nome");

It would need to make this process not crash, and work in the background...

  • What stops android query, php or java?

  • Android consults a php page that the same query the BD and is received on Android. Works well, but hangs.

  • Where does Voce have the server? Does it have a good network connection? What about android? I send XML files and it’s relatively fast. It crashes or takes time?

  • The server is Pretty Fast the problem is that the app hangs when connecting until receiving the message, that is if I make a loop that updates all the time making several connections then the app would crash without stopping.....

  • Ah that, you have to put your service on android as a service.

1 answer

1


You can create a subclass of the class AsyncTask.

private class LoginAsync extends AsyncTask<String, Integer, Boolean> {
    protected String doInBackground(String... parametros) {
       String campoLogin = parametros[0];
       String campoSenha = parametros[1];
       Conectar Conn = new Conectar("http://meusite.com/chat/login.php?login=" + campoLogin + "&senha=" + campoSenha );

       JSONArray array = new JSONArray(Conn.response);
       JSONObject object = array.getJSONObject(0);
       id = (String) object.get("id");
       nome = (String) object.get("nome");
       return nome != null;
 }

   protected void onProgressUpdate(Integer... progresso) {
       // Mostre uma barra de carregamento de acordo com o progresso.
   }

   protected void onPostExecute(Boolean login) {
       showDialog("Login com sucesso: " + login);
   }
}
  • It’s better with service, always have the service running...

  • Using Service is another way of doing it, but it is more complicated and is suitable for tasks that will run longer in the background. The idea of a Service is the continuous execution of some task in the background. For the execution of only one task, Asynctask is preferred.

  • Yes, but from what I understand, you’ll always be waiting for messages.

  • Maybe in the message part yes, but the code posted speaks of login in the application, run only once.

  • Maybe the login example wasn’t such a good idea, I was actually thinking of using several veses to update the chat... But the idea of the above friend seems promising, could not solve my motto?

  • I created a new class and put the following code above, then instead of using Conectar Conn = new Conectar("http://meusite.com/chat/login.php?login=" + campoLogin + "&senha=" + campoSenha ); I’m using a parameter website thus: protected Boolean doInBackground(String... parametros) {&#xA; String website = parametros[0];&#xA; Conectar Conn = new Conectar(website);&#xA; resposta = Conn.response; It works well when I call this class even more if it keeps crashing the same way.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.