Reputation: 66
I have just such a problem, the following small code image downloader where params[0]- url and params[2] - file name, this design works well on Android 5.0+, but on older versions, I get an error:
-javax.net.ssl.SSLException: Connection closed by peer
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
InputStream is = null;
try {
is = new URL(params[0]).openStream();
} catch (IOException e) {
e.printStackTrace(); // Exception there
}
bitmap = BitmapFactory.decodeStream(is);
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
OutputStream outStream = null;
File file = new File(Constans.DATA_APPLICATION_PATH+"/photo/"+params[1].replace("\"","").trim());
try {
// make a new bitmap from file
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
file.deleteOnExit();
return bitmap;
}
`
Upvotes: 1
Views: 688
Reputation: 1918
You have problem with SSL authorization, which will come for sure if you are using self signed certificates.
So the way to make android app to connect to URL is to skip the SSL verification (Only if we are sure about the connection)
For this you can add this class and execute it inside onCreate method
new NukeSSLCerts().nuke();
It will skip certificate verification by trusting all certificates.
Upvotes: 1