Android : Efficient way of getting data from MySQL instantly in Android?

on Sunday, September 14, 2014


I have a simple chat app and I want to check my database and get the new messages instantly, My code works but I do not know if this is the correct way to do it.


So I'm using a Thread inside a service and I call Thread.sleep and make it sleep for a second



class messagesThread extends Thread {
@Override
public void run() {
while (true){
try {
String response = null;
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
String url = "http://192.168.1.2/getMessages.php";
URL messagesURL = null;
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
messagesURL = new URL(url);
connection = (HttpURLConnection) messagesURL.openConnection();
connection.setConnectTimeout(3000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);

OutputStream outputStream = connection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(newOutputStreamWriter(outputStream, "UTF-8"));
bufferedWriter.write(getQuery(params));
bufferedWriter.flush();
bufferedWriter.close();


inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
stringBuffer.append(line + "\n");
while ((line = reader.readLine()) != null) {
stringBuffer.append(line + "\n");
}

response = stringBuffer.toString();

}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}finally {
if(connection!=null){
connection.disconnect();
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

if (response != null) {
try {
JSONObject jsonObj = new JSONObject(response);
if (jsonObj != null) {
JSONArray messagesArray = jsonObj.getJSONArray("MessageArray");

for (int i = 0; i < messagesArray.length(); i++) {
JSONObject result = (JSONObject) messagesArray.get(i);
//I receive data here
thereIsNewData = true;
}
}

} catch (JSONException e) {
e.printStackTrace();
thereIsNewData = false;
}
sleep(1000);
} catch (InterruptedException e) {
}
}
}
}


getMessages.php :



<?php

$host='localhost';
$uname='root';
$pwd='admin';
$db="db";
$con=mysql_connect($host,$uname,$pwd);
if(!$con){
die("connection failed");
}

mysql_select_db($db,$con);
$response = array();
$response["MessageArray"] = array();
$id = $_REQUEST['id'];

$getMSG = mysql_query("SELECT * FROM Messages WHERE Receiver = '$id' ",$con);
while($row = mysql_fetch_array($getMSG)){
$tmp = array();
$tmp['sender'] = $row['Sender'];
$tmp['msg'] = $row['Message'];
array_push($response["MessageArray"], $tmp);

}
echo json_encode($response);
?>


Isn't this heavy on the server? Because when I run my app and go to MySQL the page lags and it's loading forever


so I was wondering if there's a way more efficient than this


Thank you !


0 comments:

Post a Comment