1

I have an Android app that downloads a JPG from a server every so often, and I need to be able to display a "Down for Maintenance" image whenever a certain file is present on the internet. Example: Phone checks for "yes.maint" or some other file on a web server - if present, it shows maintenance image instead of other image. If not present, load other image as normal.

Is this possible in Android?

Thanks

Ross
  • 173
  • 1
  • 2
  • 12

2 Answers2

0

I dont think this is the best way to implement some thing like this This sounds more like a quick and dirty solution but it is possible

anyway here is an example class for http download it should do the trick

http download java get
//------------------------------------------------------------//
//  JavaGetUrl.java:                                          //
//------------------------------------------------------------//
//  A Java program that demonstrates a procedure that can be  //
//  used to download the contents of a specified URL.         //
//------------------------------------------------------------//
//  Code created by Developer's Daily                         //
//  http://www.DevDaily.com                                   //
//------------------------------------------------------------//

import java.io.*;
import java.net.*;

public class JavaGetUrl {

   public static void main (String[] args) {

      //-----------------------------------------------------//
      //  Step 1:  Start creating a few objects we'll need.
      //-----------------------------------------------------//

      URL u;
      InputStream is = null;
      DataInputStream dis;
      String s;

      try {

         //------------------------------------------------------------//
         // Step 2:  Create the URL.                                   //
         //------------------------------------------------------------//
         // Note: Put your real URL here, or better yet, read it as a  //
         // command-line arg, or read it from a file.                  //
         //------------------------------------------------------------//

         u = new URL("http://200.210.220.1:8080/index.html");

         //----------------------------------------------//
         // Step 3:  Open an input stream from the url.  //
         //----------------------------------------------//

         is = u.openStream();         // throws an IOException

         //-------------------------------------------------------------//
         // Step 4:                                                     //
         //-------------------------------------------------------------//
         // Convert the InputStream to a buffered DataInputStream.      //
         // Buffering the stream makes the reading faster; the          //
         // readLine() method of the DataInputStream makes the reading  //
         // easier.                                                     //
         //-------------------------------------------------------------//

         dis = new DataInputStream(new BufferedInputStream(is));

         //------------------------------------------------------------//
         // Step 5:                                                    //
         //------------------------------------------------------------//
         // Now just read each record of the input stream, and print   //
         // it out.  Note that it's assumed that this problem is run   //
         // from a command-line, not from an application or applet.    //
         //------------------------------------------------------------//

         while ((s = dis.readLine()) != null) {
            System.out.println(s);
         }

      } catch (MalformedURLException mue) {

         System.out.println("Ouch - a MalformedURLException happened.");
         mue.printStackTrace();
         System.exit(1);

      } catch (IOException ioe) {

         System.out.println("Oops- an IOException happened.");
         ioe.printStackTrace();
         System.exit(1);

      } finally {

         //---------------------------------//
         // Step 6:  Close the InputStream  //
         //---------------------------------//

         try {
            is.close();
         } catch (IOException ioe) {
            // just going to ignore this one
         }

      } // end of 'finally' clause

   }  // end of main

} // end of class definition
sherif
  • 2,282
  • 19
  • 21
  • or take a look at this http://stackoverflow.com/questions/4596447/java-check-if-file-exists-on-remote-server-using-its-url – sherif Sep 03 '11 at 20:11
0

Assuming the file "yes.maint" or some of other file is accessible via HTTP, you could use HTTPClient and call execute to get the HTTPResponse. You then call getStatusLine() to check and get the status code for that from StatusLine getStatusCode(). If it is not found, hopefully you get 404 that you could use to check which image you have to load

momo
  • 21,233
  • 8
  • 39
  • 38