• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Android studio store online data once to use your app offline?

Cripdk

Lurker
Nov 18, 2017
2
1
Hello,

I'm new in this forum and also at android development.I'm searching a lot about database and android app but i can't find any solution about my problem.I want to build an app that when the device is online get the data from a database and load it to a spinner then save the data locally and when the device is offline use the saved data and when the device get's online check for changes and update the "local database" for next time.

Sorry for my English but i don't know a lot...so if is any misunderstanding tell me.

Thank you
 
Hi. What part of this are you asking for help with?
There are several elements to the solution. Start with just a local database. There are many web resources explaining how to do this.
If you have specific problems, feel free to ask for advice in this forum.
Hello LV426 and thanks for your response. let me explain more about my project. I have build an online database on internet that some times the array values changes remotely from an other app. I want to build an app that when the phone is connected to internet at first time syncronize this database and store all arrays and values to a local database. Now I'm reading all this data with this code :

Code:
  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            final List<String> list1 = new ArrayList<String>();


            try {
                URL url = new URL("My Link/get_Json.php");
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("POST");
                urlConnection.connect();
                is = urlConnection.getInputStream();
            } catch (Exception e) {
            }

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
            }

            try {
                JSONArray JA = new JSONArray(result);
                JSONObject json = null;
                name = new String[JA.length()];

                for (int i = 0; i < JA.length(); i++) {
                    json = JA.getJSONObject(i);
                    name[i] = json.getString("name");
                }

                for (int i = 0; i < name.length; i++) {
                    list1.add(name[i]);
                }
                spinner_fn();
            } catch (Exception e) {
                textView.setText("error");

            }

and add all this data to a spinner :
Code:
 private void spinner_fn() {
        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, name);
        dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        onlinespinner.setAdapter(dataAdapter1);
    }

That's make the app running only when is connected to internet. If I try to run it without internet i get "nothing" on my spinner show i think i have to store the "name[]" arrayto a local array that doesn't lose the data when i close the app and use the local stored array when the device is offline. sorry if i missunderstanding somthing but as i sed i'm new in android development and i'm trying to learn it my self.
 
  • Like
Reactions: keklou1er
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones