我正在运行一个Android程序来通过URL加载图像,但它给了我SSLHandShakeException。任何人都可以建议我应该在代码中添加什么来删除它?
public class MainActivity extends Activity {
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageload);
LoadImageFromURL loadImage = new LoadImageFromURL();
loadImage.execute();
}
public class LoadImageFromURL extends AsyncTask<String, Void, Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
try {
URL url = new URL("https://www.babyboomapps.com/members/upload-album-files/1436952341Hydrangeas.jpg");
/* InputStream is = url.openConnection().getInputStream();
Bitmap bitMap = BitmapFactory.decodeStream(is);
compress(bitMap);*/
downloadImage(""+url);
return null;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
iv.setImageBitmap(result);
}
}
public void compress(Bitmap bitMap){
Bitmap original = bitMap;
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.PNG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
Log.e("Original dimensions", original.getWidth()+" "+original.getHeight());
Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight());
}
/* public void hostnameverifier() throws UnknownHostException, IOException{
SocketFactory sf = javax.net.ssl.SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) sf.createSocket("babyboom.com", 8443);
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
SSLSession s = socket.getSession();
// Verify that the certicate hostname is for mail.google.com
// This is due to lack of SNI support in the current SSLSocket.
if (!hv.verify("babyboom.com", s)) {
throw new SSLHandshakeException("Expected babyboom.com" + s.getPeerPrincipal());
}
// At this point SSLSocket performed certificate verificaiton and
// we have performed hostname verification, so it is safe to proceed.
// ... use socket ...
socket.close();
}*/
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 4;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpsURLConnection httpConnection = (HttpsURLConnection) connection;
httpConnection.setRequestMethod("POST");
httpConnection.connect();
Log.d(getClass().getName(), "response code:--- "+httpConnection.getResponseCode());
if (httpConnection.getResponseCode() == HttpsURLConnection.HTTP_OK ||
httpConnection.getResponseCode() == HttpsURLConnection.HTTP_NOT_MODIFIED) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
}