timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@override
public void run() {
Conn_status_handler();
}
}, 0, 2000);
public void Conn_status_handler(){
class GetDataJSON extends AsyncTask<String, Void, String> {
@override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
// HttpPost httppost = new HttpPost("
http://myweb.temiloluwaodewumi.com.ng/autochatfetch.php");
HttpPost httppost = new HttpPost("
http://10.0.3.2/mychat/autochatfetch.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
// final int status = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch(ClientProtocolException x){
x.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@override
protected void onPostExecute(String result){
//loader.setVisibility(View.VISIBLE);
if(result != null){
chat_auto_fetch_updater(Temp, Username);
//Temp is the user and Username is the receiver;
}
else
{
networkmsg.setVisibility(View.VISIBLE);
networkmsg.setText("NO INTERNET CONNECTION...CHECK INTERNET");
}
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
private void chat_auto_fetch_updater(String username,String susername) {
Response.Listener<String> resposelistener = new Response.Listener<String>() {
@override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
boolean success = jsonObject.getBoolean("success");
String x = jsonObject.getString("msg");
String y = jsonObject.getString("alertme");
if (success) {
if(chatCounter.equals(x)){
// Toast.makeText(chat.this, "No New Chat", Toast.LENGTH_SHORT).show();
}
else{
// Toast.makeText(chat.this, "New Chat", Toast.LENGTH_SHORT).show();
chatCounter = x;
if(y.equals(Temp)){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
}
personer.clear();
getData2();
//Toast.makeText(getBaseContext(),"updater"+updater,Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(chat.this, "Status Failed", Toast.LENGTH_SHORT).show();
//loader.setVisibility(View.GONE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
auto_chat_fetch log = new auto_chat_fetch(username,susername, resposelistener);
RequestQueue queue = Volley.newRequestQueue(chat.this);
// Toast.makeText(chat.this, Temp+"Auto Status", Toast.LENGTH_SHORT).show();
queue.add(log);
Please carefully look at my code ....Am checking for new message in the phpdatabase which will refresh the listviewadapter upon a new message seen