0

I want to create an online queue monitoring application which it will show the new data entered to the database.The UI sample is below

UI

I want the Current Serving Ticket to update every second if there is a new data entered(Json).

Here the MainActivity.java

public class MainActivity extends AppCompatActivity {

public static TextView data;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button input_number = (Button) findViewById(R.id.button2);
    Button reminder = (Button) findViewById(R.id.button);

    final TextView userinputtext = (TextView) findViewById(R.id.textView6);

    data = (TextView)findViewById(R.id.textView10);

Here is the data.java that i created.

public class data extends AsyncTask<Void,Void,Void> {

String data=" ";
String singleParsed = " ";

@Override
protected Void doInBackground(Void... voids) {
    try {

        URL url = new URL("url/SamplePage.php");
        HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line ="";
        while(line!=null){
            line = bufferedReader.readLine();
            data = data + line;
        }

        JSONArray JA = new JSONArray(data);
        for (int i =0; i<JA.length();i++){
            JSONObject JO = (JSONObject) JA.get(i);

            singleParsed = JO.get("ticket_number")+"";

            //dataParsed = dataParsed + singleParsed;

        }

    }catch (MalformedURLException e){
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }catch (JSONException e){
        e.printStackTrace();
    }
    return null;

}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    MainActivity.data.setText(this.singleParsed);

}

}

If there is any comment or help I would appreciate. Thanks.

Android
  • 1,420
  • 4
  • 13
  • 23
Roshan Dx
  • 39
  • 5

2 Answers2

0

The fastest answer would be setting a timer each second to query the database for data, and update the view. If you're looking for a more optimized, complete, documented and easier solution, I suggest you use firebase, which offers services like Realtime database : https://youtu.be/U5aeM5dvUpA Of course there are other alternatives like RethinkDB. You should also look up websocket since they are similar approach to this kind of real time update problem. But firebase is perfect for this in my opinion.

Moez ‌
  • 61
  • 5
  • Hi, thank you for your reply. I am currently using Amazon Web Services. I manage to view the new data whenever I insert into the database but I have to restart the application for it to happen. The Json sample : [{"id":1,"ticket_number":"1008","reg_date":"2018-11-27 19:00:00"},{"id":2,"ticket_number":"1009","reg_date":"2018-11-27 19:01:00"},{"id":3,"ticket_number":"1010","reg_date":"2018-11-27 19:02:00"},{"id":4,"ticket_number":"1011","reg_date":"2018-11-27 19:03:00"}] – Roshan Dx Jan 07 '19 at 11:59
  • Hi, do you mean that after you insert new data the DB (manually, from the AWS I suppose), you can only view it after you restart tha application ? If that's the case, that's normal, since the TextView gets updated only when you tell it to. That's why you could tell it to update itself as long as the app is running every second using a timer (Easier and faster solution). The answer by @FahimSalamChowdhury is helpful, there's something called AWS Mobile Hub, (details in next comment) that could help you get a better way of doing it with AWS. I trust you will do the right choice. – Moez ‌ Jan 07 '19 at 12:20
  • https://stackoverflow.com/questions/39589636/what-would-be-the-aws-equivalent-to-firebase-realtime-database – Moez ‌ Jan 07 '19 at 12:20
  • The Current Serving Ticket number should be updated automatically whenever a new data entered (reading from php file in Json format). For now I manually entered the data to see if the Current Serving Ticket updated or not but no. So your suggestion is to add timer ? – Roshan Dx Jan 07 '19 at 12:41
  • Yes, for now you can do that for this pharmacy project. Create a method `updateCurrentServingTicket() {//Update TextView}` and call it in the timer you are going to implement. However, if you use a websocket, you can listen to changes happening in the JSON Array 'url/SamplePage.php' when an event 'ticketAdded' is emitted. Your app will pick that up and call the `updateCurrentServingTicket()`, so no need for a timer. Here's a link on Websockets for android : https://github.com/koush/android-websockets , they require a little more code than a timer, but they are the better option. Good luck! – Moez ‌ Jan 07 '19 at 12:52
  • Should i create the method in MainActivity or data.java ? Sorry for asking – Roshan Dx Jan 07 '19 at 13:28
  • There's abolustely no problem, you're more than welcome to ask anything ! And from your code I would create it in MainActivity for better understanding of the code, and since you're getting your views in there anyway, so it's easier to update it when calling the `updateCurrentServingTicket()` method. Good luck ! – Moez ‌ Jan 07 '19 at 13:34
0

You can use SyncAdapter and LiveData. If you use Room, it will be more easier to handle.