Problems with AsyncTask crash the application

2

I'm developing an online chat that should update itself all the time, the problem is that every query hangs the application for a few seconds. I had already asked a similar question in: How to make connections to a php work in background in Android but it was not exactly what I wanted.

I need pages not to crash the app in every query . I've heard about backgroundWoker and Service but honestly no I have no idea what I might be doing wrong.

I'm using MySQL + PHP + JSON.

Here's my code:

package com.Dannark.livechat;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

class Connect extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Log.e("-- MYSQL --",result);
        //Do anything with response..
    }
}

EDIT: And I'm interacting with the connect class with the Connect class like this:

package com.Dannark.livechat;

import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;

public class Conectar {

    String response;
    int status;

    public Conectar(String website){
        //First - send request to server
        AsyncTask<String,String,String> task;
        String uri = website;       //  www.meusite.com/get_chat/?room=10


        try {
            task = new Connect().execute(uri);
            response = task.get();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

And finally the class conectar this way:

Conectar Conn = new Conectar("http://animesslife.engine001.com/chat/login.php?login=" + campoLogin.getText() + "&senha=" + campoSenha.getText() );
Log.i("-RESPOSTA-",Conn.response); //Conn.response seria a resposta no decodificada em JSON

Should I use another method instead of this?

    
asked by anonymous 21.05.2014 / 20:40

1 answer

2

AsyncTask was made to run the "heavy" code on the InBackground () method and the UI (refresh the screen) code in the onPostExecute () method What you are doing when executing this get () method is to perform a heavy operation synchronously on the MainThread, which causes 'hang' because the Main Thread gets busy with the operation.

What you should do is just call the execute () method, and within onPostExecute () you will get the result of the heavy operation as parameter (the return of the and you can update the screen in. That is, in onPostExecute () you will do something like: textView.setText (dataResult)

You have an example here: link

    
21.05.2014 / 21:01