1
由於證書錯誤,我無法使用HTTPS協議下載圖像。這發生了幾個星期或更長的時間,在此之前,我沒有任何問題。使用Android的HTTPS從Facebook Graph API下載圖像時出錯
如果我使用HttpCommons Apache庫,錯誤是「主機名證書不匹配」,如果我使用URL和URLConnection,則例外是「證書未驗證」。
我很絕望,因爲我需要完成這項工作來完成我的項目,而我所測試的所有項目都無法工作。
這是Apache的庫中的代碼:
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try
{
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
HttpEntity entity = response.getEntity();
if (entity != null)
{
try
{
byte[] data = EntityUtils.toByteArray(entity);
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//bitmap = BitmapFactory.decodeStream(entity.getContent());
} catch (IOException e)
{
e.printStackTrace();
Log.e("FACEBOOKLIBRARY", "Error fetching image from this URL: " + url
+ "because of the download is corrupted");
bitmap = null;
} finally
{
entity.consumeContent();
}
}
}
else
{
Log.e("FACEBOOKLIBRARY", "Error fetching image from this URL: " + url
+ " because of the error: " + response.getStatusLine().toString());
bitmap = null;
}
} catch (IOException e)
{
e.printStackTrace();
Log.e("FACEBOOKLIBRARY", "Error fetching image while connecting to this URL: " + url);
bitmap = null;
}
return bitmap;
這是URL代碼:
try {
URL u = new URL(url);
URLConnection connection = u.openConnection();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1) {
baf.append(current);
}
byte[] byteArray = baf.toByteArray();
bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
bitmap = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
bitmap = null;
}