2011-09-09 12 views
0

パスワードで保護されたフォルダからAndroidアプリにイメージファイルをダウンロードする方法についてのヘルプが必要です。私が持っているコードは、getInputStream/BufferedInputStreamと一緒にURLConnectionを使用していますが、そこにユーザ名/パスワード認証を取得する方法はわかりません。私はHttpClientにUsernamePasswordCredentialsがあることを知っていますが、HttpClientを使ってファイルをダウンロードする方法が分かりませんので、それほど助けにはなりません。Android Appの保護されたWebフォルダからファイルをダウンロードする方法は?

これまでに見つかったコードはありますが、これを使用してファイルをダウンロードするにはどうすればよいですか?

public class ClientAuthentication { 

    public static void main(String[] args) throws Exception { 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     try { 
      httpclient.getCredentialsProvider().setCredentials(
        new AuthScope("localhost", 443), 
        new UsernamePasswordCredentials("username", "password")); 

      HttpGet httpget = new HttpGet("https://localhost/protected"); 

      System.out.println("executing request" + httpget.getRequestLine()); 
      HttpResponse response = httpclient.execute(httpget); 
      HttpEntity entity = response.getEntity(); 

      System.out.println("----------------------------------------"); 
      System.out.println(response.getStatusLine()); 
      if (entity != null) { 
       System.out.println("Response content length: " + entity.getContentLength()); 
      } 
      EntityUtils.consume(entity); 
     } finally { 
      // When HttpClient instance is no longer needed, 
      // shut down the connection manager to ensure 
      // immediate deallocation of all system resources 
      httpclient.getConnectionManager().shutdown(); 
     } 
    } 
} 

それとも、私は、ファイルのダウンロードのためにこのコードを持っている - どのように私はこれに資格情報を追加します。おかげで

http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device

を!

編集:よく、ここで助けを得ていない。私は私の目的のために再試行しようとしているこの答えを見つけました:Download a file with DefaultHTTPClient and preemptive authentication

+0

「https:// user:password @ host.domain/page /」のようなURLに入れてみましたか? –

+0

はい、私はファイルioの例外を与えます。しかし、iOSでうまくいった! –

+1

例外理由は何ですか? –

答えて

2

これは私が思い付いたコードです。私はそれがウェブ上のどこでもこのようなコードを見つけるのに非常に苦労して以来、私はそれを投稿しています。

public void downloadHTTPC(Activity act, String imageURL, String fileName) { 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    try { 
     String pathDir = act.getExternalFilesDir(null).toString() + "/" + fileName; 
     File file = new File(pathDir); 
     long startTime = System.currentTimeMillis(); 
     httpclient.getCredentialsProvider().setCredentials(
       new AuthScope(null, -1), 
       new UsernamePasswordCredentials("user", "password")); 

     HttpGet httpget = new HttpGet(IMGURL + imageURL); 

     System.out.println("executing request" + httpget.getRequestLine()); 
     HttpResponse response = httpclient.execute(httpget); 
     HttpEntity entity = response.getEntity(); 

     System.out.println("----------------------------------------"); 
     System.out.println(response.getStatusLine()); 

     if (entity != null) { 
      System.out.println("Response content length: " + entity.getContentLength()); 
      InputStream is = entity.getContent(); 

      BufferedInputStream bis = new BufferedInputStream(is); 

      /* 
      * Read bytes to the Buffer until there is nothing more to read(-1). 
      */ 
      ByteArrayBuffer baf = new ByteArrayBuffer(50); 
      int current = 0; 
      while ((current = bis.read()) != -1) { 
       baf.append((byte) current); 
      } 

      /* Convert the Bytes read to a Stream. */ 
      FileOutputStream fos = new FileOutputStream(file); 
      fos.write(baf.toByteArray()); 
      fos.close(); 
      Log.d("ImageManager", "download ready in" 
        + ((System.currentTimeMillis() - startTime)/1000) 
        + " sec"); 
     } 

     //EntityUtils.consume(entity); 
    } catch (IOException e) { 
     Log.d("ImageManager", "Error: " + e); 

    } finally { 
     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     httpclient.getConnectionManager().shutdown(); 
    } 
} 
関連する問題