2

So I'm building an app with a lot of web content I plan to release it using Phone Gap build but will host all the content online and will link to it. I was wondering if there is a way that the web pages can be downloaded when there is an active internet connection for offline use and when there is a connection again for the data to be refreshed preferably when the user is using a wifi connection. The site will mostly be in html, js, and php. I will be hosting with bluehost

Is there any way of doing this? Thanks in advance! Littleswany!

littleswany
  • 473
  • 2
  • 6
  • 18

3 Answers3

2

I am not familiar with java, but i think i can provide the logic to get the job done.

You want to do an infinite loop that checks if the user is on wifi. Then if true, use wget, rsync, or scp to download the website. Something like this.:

while (true){
    // do an if statement that checks if user is on wifi. Then do a then statement that uses rsync or wget. 
}

Info on how to nest if statements in while loops in java: java loop, if else

I do not know if wget, rsync, or scp can be ran from java. You'll need to look more into it or write your own alternative function to do it. Something like:

function download_file() {
       var url = "http://www.example.com/file.doc"
       window.location = url;
     }

You should be able to do it from your java like this:

String whatToRun = "/usr/local/bin/wget http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe";

Sources: 1. What is the equivalent of wget in javascript to download a file from a given url? 2. Call a command in terminal using Java (OSX)

Community
  • 1
  • 1
PhysicalChemist
  • 540
  • 4
  • 14
2

First Create an Connection filter class

public class Connection_Status{
    private static ConnectivityManager connectivityManager;
    static boolean connected = false;

    public static Boolean isOnline(Context ctx) {
        try {
            connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            connected = networkInfo != null && networkInfo.isAvailable()&& networkInfo.isConnected();
            return connected;
        } catch (Exception e) {
            System.out.println("CheckConnectivity Exception: " + e.getMessage());
        }
        return connected;
    }
}

And in your Main class

    public class Main extends Activity{
       private WebView mWebView;

      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

       mWebView = (WebView) findViewById(R.id.webview);
       mWebView.getSettings().setJavaScriptEnabled(true);
       mWebView.getSettings().setBuiltInZoomControls(true);

if(Connection_Status.isOnline(Main.this)){

    HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
    HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do
    HttpResponse response = httpclient.execute(httpget); // Executeit
    HttpEntity entity = response.getEntity(); 
    InputStream is = entity.getContent(); // Create an InputStream with the response
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) // Read line by line
        sb.append(line + "\n");

    String resString = sb.toString(); //

    is.close(); // Close the stream
             }
        }
   }

Or you can use cache on it e.g

       mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);
       mWebView.getSettings().setAppCachePath(""+this.getCacheDir());
       mWebView.getSettings().setAppCacheEnabled(true); 
       mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 

Don't forget to add the following permissions

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- for the connection status-->

Sources: https://stackoverflow.com/a/6503817/1309629

Community
  • 1
  • 1
Cjames
  • 1,852
  • 1
  • 20
  • 23
2

PhoneGap apps ARE downloaded to the device, when they are downloaded from the store. They are basically a wrapper around an index.html file, but the app is actually programmed in JavaScript, which is responsible for creating and displaying views etc. The only time you need to check for an internet connection is when you are communicating with your back end (PHP)... If the ajax request fails, the best solution is to provide the user with a button/link to try again when they have regained their internet connection, or set a timer which fires intermittently to keep trying again... NEVER use a while(true) loop in your Phone Gap app - it will just hang.

evilunix
  • 960
  • 6
  • 11
  • PS you will only be hosting your PHP on bluehost. The JavaScript/HTML/CSS is all downloaded with the app from the store. – evilunix Sep 04 '14 at 20:26