Android:下载JPG if语句

时间:2011-09-03 19:59:23

标签: android webserver jpeg

我有一个Android应用程序,每隔一段时间就会从服务器下载一个JPG,每当互联网上出现某个文件时,我都需要能够显示“Down for Maintenance”图像。 例: 电话检查“yes.maint”或Web服务器上的某些其他文件 - 如果存在,则显示维护图像而不是其他图像。如果不存在,请正常加载其他图像。

这在Android中可行吗?

由于

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

假设文件“yes.maint”或某些其他文件可通过HTTP访问,您可以使用HTTPClient并调用execute来获取HTTPResponse。然后调用getStatusLine()来检查并从StatusLine getStatusCode()获取该状态代码。如果找不到,希望你得到404可以用来检查你必须加载的图像