2
I am developing a online chat which should update alone all the time, the problem is that each query hangs the application for a half seconds. I had already asked a similar question in: How to make connections to a php work in the background on Android but it wasn’t exactly what I wanted.
I need you to pages did not lock the app on each query. I’ve heard of backgroundWoker and Service but I honestly have no idea what I might be doing wrong.
I am using Mysql+PHP+JSON.
This is 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 class connect With class Connect in that way:
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
in 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
I should use another method instead of this?
How are you interacting with the "Connect" class? Apparently I’m not seeing anything wrong with Asynctask, except that of not properly closing resources in a "Finally block".
– Alécio Carvalho
I added some information...
– Dannark
I added the answer. I’m answering from my smartphone, so you can’t put the example code, but I hope you get the idea.
– Alécio Carvalho