Android: Скачать JPG, если заявление - PullRequest
1 голос
/ 03 сентября 2011

У меня есть приложение для Android, которое загружает JPG с сервера очень часто, и мне нужно иметь возможность отображать изображение «Неисправность для обслуживания» всякий раз, когда определенный файл присутствует в Интернете. Пример: Телефон проверяет «yes.maint» или какой-либо другой файл на веб-сервере - если он присутствует, он показывает изображение обслуживания вместо другого изображения. Если нет, загрузите другое изображение как обычно.

Возможно ли это в Android?

Спасибо

Ответы [ 2 ]

0 голосов
/ 04 сентября 2011

Предполагая, что файл "yes.maint" или какой-либо другой файл доступен через HTTP, вы можете использовать HTTPClient и вызвать execute для получения HTTPResponse . Затем вы вызываете getStatusLine (), чтобы проверить и получить код состояния для этого из StatusLine getStatusCode () . Если он не найден, надеюсь, вы получите 404, который вы можете использовать, чтобы проверить, какое изображение вы должны загрузить

0 голосов
/ 04 сентября 2011

Я не думаю, что это лучший способ реализовать что-то подобное Это больше похоже на быстрое и грязное решение, но это возможно

в любом случае, вот пример класса для загрузки 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
...