title: How to update ui if there is new data in response using volley?
body: <p>I have a code which gets data from the web service and displays the response in a textview. However, If the data changes in the webservice, I want to be able to update my textview with the new data and possibly play a notification sound indicating that there is a new data. I have tried doing polling, in which I will send request to the web service for data every x second. The problem though, is that after a few minutes, my app crashes because of out of memory. Also, regarding the notification sound, my idea was that, if the textview value changes, only then will the sound play or an alert dialog will appear. What happened in my case was that, since I am polling every x second, then the textview refreshes every x second as well, resulting in the alert dialog box appearing every x second. What is the best method for me to use to achieve what I want?</p>
  
  <p>ParseJson</p>
  
  <pre><code>public class ParseJson {
  public static String[] playing;
  
  public static final String JSON_ARRAY = "result";
  public static final String RESULT_ID = "playing";
  
  private JSONArray users = null;
  
  private String json;
  
  public ParseJson(String json){
      this.json = json;
  }
  
  public void parseJSON(){
      JSONObject jsonObject=null;
      try {
          jsonObject = new JSONObject(json);
          users = jsonObject.getJSONArray(JSON_ARRAY);
  
          playing = new String[users.length()];
  
          for(int i=0;i&lt;users.length();i++){
              JSONObject jo = users.getJSONObject(i);
              playing[i] = jo.getString(RESULT_ID);
          }
      } catch (JSONException e) {
          e.printStackTrace();
      }
      }
  } 
  </code></pre>
  
  <p>MainActivity</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  public TextView text;
  
  MediaPlayer mp;
  
  public static final String JSON_URL = "http://172.16.51.118/androidphp/data.php";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      text = (TextView) findViewById(R.id.text);
      text.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  
          }
  
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
              new AlertDialog.Builder(MainActivity.this)
                      .setTitle("Response")
                      .setMessage("text has changed")
                      .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                              // continue with delete
                          }
                      })
                      .setIcon(android.R.drawable.btn_star)
                      .show();
          }
  
          @Override
          public void afterTextChanged(Editable s) {
  
          }
      });
  
      handleMe();
  }
  
  public void handleMe() {
      Timer timer = new Timer();
      final Handler handler = new Handler(){
  
  
          @Override
          public void handleMessage(Message msg) {
              // do something on UI
  
          }
  
      };
      TimerTask task = new TimerTask () {
          @Override
          public void run () {
              //send volley request here
              //RequestData requestData = new RequestData(MainActivity.this);
              //requestData.sendRequest(text);
              //text.setText(ParseJson.playing[0]);
              /*(RequestData requestData = new RequestData(MainActivity.this);
              requestData.sendRequest();*/
              sendRequest();
          }
      };
      timer.schedule(task, 0, 10000); // 60000 is time in ms
  }
  
  public void sendRequest(){
  
      //While the app fetched data we are displaying a progress dialog
      //final ProgressDialog loading = ProgressDialog.show(this,"Fetching Data","Please wait...",false,false);
  
      StringRequest stringRequest = new StringRequest(JSON_URL,
              new Response.Listener&lt;String&gt;() {
                  @Override
                  public void onResponse(String response) {
  
                      //text.setText(response);
  
                      //loading.dismiss();
                      showJSON(response);
                  }
              },
              new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      //Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                  }
              });
  
      RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
      requestQueue.add(stringRequest);
  }
  
  private void showJSON(String json){
      ParseJson pj = new ParseJson(json);
      pj.parseJSON();
      text.setText(ParseJson.playing[0]);
      }
  }
  </code></pre>