0

I have a java code that accepts jsonObject from php server to my android application by sending a data to the server:

requestQueue = Volley.newRequestQueue(this);

    request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);

                user_id = jsonObject.getJSONObject("user").getInt("user_id");;
                textView.setText(jsonObject.getString("msg"));

                saveInfo();

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //Toast.makeText(context, "Please check your internet connection.", Toast.LENGTH_LONG).show();
            AlertDialog.Builder a_builder = new AlertDialog.Builder(getApplicationContext());
            a_builder.setMessage("Please check your internet connection.").setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            AlertDialog alertDialog = a_builder.create();
            alertDialog.setTitle("Network failure!");
            alertDialog.show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> hashMap = new HashMap<String, String>();

            hashMap.put("username", "testing");
            hashMap.put("password", "testing");

            return hashMap;
        }
    };
    requestQueue.add(request);

Now, How can I receive jsonObject from the server in real time without any user actions? automatically update the data from my android app when the data from the server changes.

Dylan
  • 1,121
  • 1
  • 13
  • 28
  • In iOS there is a thing called Push Notifications. Which notifies apps to do something on their end. Like when you get a prompt that someone has messaged you on facebook – Brett Nov 07 '17 at 01:30
  • The question is, When **YOU** want to happen this? Periodically? Depending on device events? Depending on GPS coordinates? What? – Northern Poet Nov 07 '17 at 01:34
  • @NorthernPoet depending when the server sends a new json data, I want my application to always receive that data every time. – Dylan Nov 07 '17 at 02:48
  • So, the answer is `Service` and `Periodic requests`. Refer this https://stackoverflow.com/questions/10420358/android-periodic-background-service-advice answer for details. – Northern Poet Nov 07 '17 at 04:53

0 Answers0