パスワードで保護されたフォルダから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
「https:// user:password @ host.domain/page /」のようなURLに入れてみましたか? –
はい、私はファイルioの例外を与えます。しかし、iOSでうまくいった! –
例外理由は何ですか? –