2011-09-03 31 views
1

我有一個Android應用程序,每隔一段時間從服務器上下載一個JPG文件,並且每當某個文件出現在互聯網上時,我需要能夠顯示「維護下來」圖像。 例如: 電話檢查「yes.maint」或Web服務器上的其他文件 - 如果存在,它將顯示維護圖像而不是其他圖像。如果不存在,請正常加載其他圖像。Android:下載JPG語句

這是可能的Android?

感謝

回答

0

假設文件「yes.maint」或其他一些文件是通過HTTP訪問,您可以使用HTTPClient並調用執行,以獲得HTTPResponse。然後您調用getStatusLine()來檢查StatusLine getStatusCode()的狀態代碼。如果找不到,希望你得到404,你可以用它來檢查你必須加載的圖像

0

我不認爲這是實現這樣 這聽起來更像是一個快速和骯髒的解決方案的一些事情最好的辦法,但它可能

反正這裏是HTTP下載的示例類它應該做的伎倆

 
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 
+0

或看看這個http://stackoverflow.com/questions/4596447/java-check-if-file -exists的遠程服務器上,使用其通網址 – sherif