Advertisement
Guest User

sp

a guest
Nov 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. package com.giftypetech.realmwithvolley;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.annotation.SuppressLint;
  6. import android.content.Context;
  7. import android.content.SharedPreferences;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.widget.TextView;
  11.  
  12. import com.android.volley.Request;
  13. import com.android.volley.Response;
  14. import com.android.volley.VolleyError;
  15. import com.android.volley.toolbox.JsonObjectRequest;
  16. import com.android.volley.toolbox.StringRequest;
  17.  
  18. import org.json.JSONArray;
  19. import org.json.JSONException;
  20. import org.json.JSONObject;
  21.  
  22. import java.util.ArrayList;
  23. import java.util.List;
  24.  
  25. public class MainActivity extends AppCompatActivity {
  26.  
  27. public final String MY_SREFS_NAME = "my";
  28. //final String VERSION = "updateVersion";
  29. //String VERSION;
  30. SharedPreferences preferences;
  31. TextView textView;
  32. int localVersion;
  33. int remoteVersion;
  34.  
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39. textView = findViewById(R.id.textView);
  40. versionLoadData();
  41. }
  42.  
  43. /* This method for version api data fetch and save that version on SharedPreferences */
  44. private void versionLoadData() {
  45. String url = "";
  46.  
  47. /* This api json data :
  48. {"database":{"version": "1.0.0"}} */
  49. JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
  50. (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
  51. @SuppressLint("WrongConstant")
  52. @Override
  53. public void onResponse(JSONObject response) {
  54. try {
  55. String versionString = response.getString("version").replace(".","");
  56. remoteVersion = Integer.parseInt(versionString);
  57. preferences = getSharedPreferences(MY_SREFS_NAME,Context.MODE_PRIVATE);
  58. remoteVersion = preferences.getInt("remoteVersion",remoteVersion);
  59. Log.d("response","Remote version is:- "+remoteVersion);
  60. localVersion = preferences.getInt("localVersion", -1);
  61. Log.d("response","First time local version is:- "+localVersion);
  62.  
  63. if (localVersion == -1) {
  64. feildsLoadData();
  65. legalsLoadData();
  66. preferences = getSharedPreferences(MY_SREFS_NAME, MODE_PRIVATE);
  67. SharedPreferences.Editor editor = preferences.edit();
  68. editor.putInt("local",remoteVersion);
  69. editor.apply();
  70. localVersion = preferences.getInt("local", remoteVersion);
  71. Log.d("response","Local version is:- "+localVersion);
  72. }else {
  73. if (remoteVersion > localVersion) {
  74. feildsLoadData();
  75. legalsLoadData();
  76. }
  77. }
  78. } catch (JSONException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. }, new Response.ErrorListener() {
  83.  
  84. @Override
  85. public void onErrorResponse(VolleyError error) {
  86. Log.d("response","is:- "+error.getMessage());
  87. }
  88. });
  89.  
  90. // Access the RequestQueue through your singleton class.
  91. MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
  92. }
  93. /* This method for fields data fetch and save data on database(Realm) */
  94. private void feildsLoadData() {
  95. String url = "";
  96.  
  97. JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
  98. (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
  99. @Override
  100. public void onResponse(JSONObject response) {
  101. Log.d("response","Fields are:- "+response);
  102. }
  103. }, new Response.ErrorListener() {
  104.  
  105. @Override
  106. public void onErrorResponse(VolleyError error) {
  107. Log.d("response","Fields are:- "+error.getMessage());
  108. }
  109. });
  110.  
  111. // Access the RequestQueue through your singleton class.
  112. MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
  113. }
  114.  
  115. /* This method for legals data fetch and save data on database(Realm) */
  116. private void legalsLoadData() {
  117. String url = "";
  118. /* This api json data :
  119. {"database":{"version": "1.0.0"}} */
  120.  
  121. JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
  122. (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
  123. @Override
  124. public void onResponse(JSONObject response) {
  125. Log.d("response","Legals are:- "+response);
  126. }
  127. }, new Response.ErrorListener() {
  128.  
  129. @Override
  130. public void onErrorResponse(VolleyError error) {
  131. Log.d("response","is:- "+error.getMessage());
  132. }
  133. });
  134.  
  135. // Access the RequestQueue through your singleton class.
  136. MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement