Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Java:FileNotFoundException使用基本身份验证从URL加载位图时_Android_Bitmap_Stream_Filenotfoundexception_Urlconnection - Fatal编程技术网

Android Java:FileNotFoundException使用基本身份验证从URL加载位图时

Android Java:FileNotFoundException使用基本身份验证从URL加载位图时,android,bitmap,stream,filenotfoundexception,urlconnection,Android,Bitmap,Stream,Filenotfoundexception,Urlconnection,我知道这里有很多这样的问题,但没有一个对我有用:( 在我的Android应用程序中,我试图从URL获取一个可绘制/位图,如下所示: public Bitmap loadBitmap(ISettings settings) { Bitmap bm = null; InputStream is = null; BufferedInputStream bis = null; final URL imageURL = new URL("http://domain.com

我知道这里有很多这样的问题,但没有一个对我有用:(

在我的Android应用程序中,我试图从URL获取一个可绘制/位图,如下所示:

public Bitmap loadBitmap(ISettings settings) {
    Bitmap bm = null;
    InputStream is = null;
    BufferedInputStream bis = null;

    final URL imageURL = new URL("http://domain.com/profile/image");
    final String authString = settings.getUserName() + ":" + settings.getPassword();
    final String authStringEnc = Base64.encodeToString(authString.getBytes(), Base64.DEFAULT);

    URLConnection conn = url.openConnection();
    conn.setRequestProperty("Authorization", "Basic " + authStringEnc);
    conn.connect();
    is = conn.getInputStream();
    bis = new BufferedInputStream(is, 8192);
    bm = BitmapFactory.decodeStream(bis);
    return bm;
}
然而,每次我启动这个婴儿,都会发生一些意想不到的事情:

java.io.FileNotFoundException: http://domain.com/profile/image
这可能会对您有所帮助,这是资源的卷曲

curl -u Username:Password http://domain.com/profile/image
以及wireshark中的输出:

GET /profile/image HTTP/1.1
Authorization: Basic BASICAUTHSTRING
User-Agent: curl/7.25.0 (i386-pc-win32) libcurl/7.25.0 OpenSSL/0.9.8u zlib/1.2.6 libidn/1.18 libssh2/1.4.0 librtmp/2.3
Host: domain.com
Accept: */*

HTTP/1.1 200 OK
Date: Mon, 21 May 2012 18:36:11 GMT
Server: Apache/2.2.14 (Ubuntu)
Set-Cookie: JSESSIONID=65178AC10C59F372FEC901EBB71F38F7; Path=/profile
Content-Disposition: attachment; filename="profile.name.png"
Content-Length: 33195
Content-Type: image/png;charset=UTF-8

‰PNG
.. just the bytes of png which I cannot enter here as it would be too long :)
如何解决这个问题

编辑#1: 捕捉从手机到网络的流量我看到了这一点,这有点明显:)-但我不知道如何正确处理:

GET /profile/image HTTP/1.1
Authorization: Basic BASICAUTHSTRING
User-Agent: Dalvik/1.4.0 (Linux; U; Android 2.3.7; Nexus One Build/GRK39F)
Host: domain.com
Connection: Keep-Alive
Accept-Encoding: gzip

HTTP/1.1 400 Bad Request    
Date: Mon, 21 May 2012 23:01:42 GMT    
Server: Apache/2.2.14 (Ubuntu)    
Vary: Accept-Encoding
Content-Length: 327    
Connection: close    
Content-Type: text/html; charset=iso-8859-1   

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.2.14 (Ubuntu) Server at domain.com Port 80</address>
</body></html>

我马上就得到了图像:终于让它工作了

我希望基本字符串是正确的-嗯,我有点错了

android.util.Base64.encodeToString(authString.getBytes(), android.util.Base64.DEFAULT)
将在末尾追加一行。因此使用

android.util.Base64.No_WRAP
旗帜


所以所有的麻烦实际上只是一个字节:)

当你用wireshark连接Java代码时会发生什么?我现在就让它工作了。明天我将添加答案。
android.util.Base64.No_WRAP