I want to download an image from the web and display it on the app by clicking a button . I've written this code in MainAcitivity.java .
package com.example.guessthecelebrity;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
public void clickFunction (View view) {
ImageDownloadTask task = new ImageDownloadTask();
Bitmap myImage;
try {
myImage = task.execute("https://www.bizain.com/content/images/wordpress/2018/04/warren_buffett-1-571x400.jpg").get();
imageView.setImageBitmap(myImage);
} catch (Exception e) {
e.printStackTrace();
}
}
public class ImageDownloadTask extends AsyncTask<String,Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream in = urlConnection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(in);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
Log.i("Result","Failed in Background");
}
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
}
}
It is showing the following error in the logcat when I click the button to download the image.
2021-08-19 20:20:26.956 32442-32484/com.example.guessthecelebrity D/EGL_emulation: app_time_stats: avg=6.08ms min=5.10ms max=8.95ms count=61
2021-08-19 20:20:27.264 32442-32567/com.example.guessthecelebrity W/System.err: javax.net.ssl.SSLHandshakeException: Chain validation failed
2021-08-19 20:20:27.265 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.SSLUtils.toSSLHandshakeException(SSLUtils.java:363)
2021-08-19 20:20:27.265 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngine.convertException(ConscryptEngine.java:1134)
2021-08-19 20:20:27.265 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngine.readPlaintextData(ConscryptEngine.java:1089)
2021-08-19 20:20:27.266 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:876)
2021-08-19 20:20:27.267 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:747)
2021-08-19 20:20:27.267 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:712)
2021-08-19 20:20:27.267 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.processDataFromSocket(ConscryptEngineSocket.java:858)
2021-08-19 20:20:27.268 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.access$100(ConscryptEngineSocket.java:731)
2021-08-19 20:20:27.268 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket.doHandshake(ConscryptEngineSocket.java:241)
2021-08-19 20:20:27.268 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket.startHandshake(ConscryptEngineSocket.java:220)
2021-08-19 20:20:27.268 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:196)
2021-08-19 20:20:27.269 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:153)
2021-08-19 20:20:27.269 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
2021-08-19 20:20:27.269 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
2021-08-19 20:20:27.269 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
2021-08-19 20:20:27.270 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
2021-08-19 20:20:27.273 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
2021-08-19 20:20:27.273 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
2021-08-19 20:20:27.273 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
2021-08-19 20:20:27.273 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
2021-08-19 20:20:27.274 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
2021-08-19 20:20:27.274 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
2021-08-19 20:20:27.274 32442-32567/com.example.guessthecelebrity W/System.err: at com.example.guessthecelebrity.MainActivity$ImageDownloadTask.doInBackground(MainActivity.java:44)
2021-08-19 20:20:27.274 32442-32567/com.example.guessthecelebrity W/System.err: at com.example.guessthecelebrity.MainActivity$ImageDownloadTask.doInBackground(MainActivity.java:37)
2021-08-19 20:20:27.274 32442-32567/com.example.guessthecelebrity W/System.err: at android.os.AsyncTask$3.call(AsyncTask.java:394)
2021-08-19 20:20:27.275 32442-32567/com.example.guessthecelebrity W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2021-08-19 20:20:27.275 32442-32567/com.example.guessthecelebrity W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
2021-08-19 20:20:27.275 32442-32567/com.example.guessthecelebrity W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2021-08-19 20:20:27.276 32442-32567/com.example.guessthecelebrity W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2021-08-19 20:20:27.276 32442-32567/com.example.guessthecelebrity W/System.err: at java.lang.Thread.run(Thread.java:920)
2021-08-19 20:20:27.276 32442-32567/com.example.guessthecelebrity W/System.err: Caused by: java.security.cert.CertificateException: Chain validation failed
2021-08-19 20:20:27.277 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.TrustManagerImpl.verifyChain(TrustManagerImpl.java:719)
2021-08-19 20:20:27.277 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.TrustManagerImpl.checkTrustedRecursive(TrustManagerImpl.java:549)
2021-08-19 20:20:27.277 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.TrustManagerImpl.checkTrustedRecursive(TrustManagerImpl.java:570)
2021-08-19 20:20:27.277 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.TrustManagerImpl.checkTrustedRecursive(TrustManagerImpl.java:615)
2021-08-19 20:20:27.278 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.TrustManagerImpl.checkTrustedRecursive(TrustManagerImpl.java:615)
2021-08-19 20:20:27.278 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:505)
2021-08-19 20:20:27.278 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:425)
2021-08-19 20:20:27.278 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.TrustManagerImpl.getTrustedChainForServer(TrustManagerImpl.java:353)
2021-08-19 20:20:27.278 32442-32567/com.example.guessthecelebrity W/System.err: at android.security.net.config.NetworkSecurityTrustManager.checkServerTrusted(NetworkSecurityTrustManager.java:94)
2021-08-19 20:20:27.279 32442-32567/com.example.guessthecelebrity W/System.err: at android.security.net.config.RootTrustManager.checkServerTrusted(RootTrustManager.java:90)
2021-08-19 20:20:27.279 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket$2.checkServerTrusted(ConscryptEngineSocket.java:163)
2021-08-19 20:20:27.279 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.Platform.checkServerTrusted(Platform.java:255)
2021-08-19 20:20:27.280 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngine.verifyCertificateChain(ConscryptEngine.java:1638)
2021-08-19 20:20:27.280 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.NativeCrypto.ENGINE_SSL_read_direct(Native Method)
2021-08-19 20:20:27.281 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.NativeSsl.readDirectByteBuffer(NativeSsl.java:569)
2021-08-19 20:20:27.281 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngine.readPlaintextDataDirect(ConscryptEngine.java:1095)
2021-08-19 20:20:27.281 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.ConscryptEngine.readPlaintextData(ConscryptEngine.java:1079)
2021-08-19 20:20:27.282 32442-32567/com.example.guessthecelebrity W/System.err: ... 27 more
2021-08-19 20:20:27.282 32442-32567/com.example.guessthecelebrity W/System.err: Caused by: java.security.cert.CertPathValidatorException: timestamp check failed
2021-08-19 20:20:27.282 32442-32567/com.example.guessthecelebrity W/System.err: at sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:135)
2021-08-19 20:20:27.283 32442-32567/com.example.guessthecelebrity W/System.err: at sun.security.provider.certpath.PKIXCertPathValidator.validate(PKIXCertPathValidator.java:222)
2021-08-19 20:20:27.284 32442-32567/com.example.guessthecelebrity W/System.err: at sun.security.provider.certpath.PKIXCertPathValidator.validate(PKIXCertPathValidator.java:140)
2021-08-19 20:20:27.285 32442-32567/com.example.guessthecelebrity W/System.err: at sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(PKIXCertPathValidator.java:79)
2021-08-19 20:20:27.285 32442-32567/com.example.guessthecelebrity W/System.err: at java.security.cert.CertPathValidator.validate(CertPathValidator.java:301)
2021-08-19 20:20:27.285 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.TrustManagerImpl.verifyChain(TrustManagerImpl.java:715)
2021-08-19 20:20:27.286 32442-32567/com.example.guessthecelebrity W/System.err: ... 43 more
2021-08-19 20:20:27.286 32442-32567/com.example.guessthecelebrity W/System.err: Caused by: java.security.cert.CertificateNotYetValidException: Certificate not valid until Sat Aug 28 20:29:17 GMT+05:30 2021 (compared to Thu Aug 19 20:20:27 GMT+05:30 2021)
2021-08-19 20:20:27.289 32442-32567/com.example.guessthecelebrity W/System.err: at com.android.org.conscrypt.OpenSSLX509Certificate.checkValidity(OpenSSLX509Certificate.java:264)
2021-08-19 20:20:27.289 32442-32567/com.example.guessthecelebrity W/System.err: at sun.security.provider.certpath.BasicChecker.verifyTimestamp(BasicChecker.java:194)
2021-08-19 20:20:27.289 32442-32567/com.example.guessthecelebrity W/System.err: at sun.security.provider.certpath.BasicChecker.check(BasicChecker.java:144)
2021-08-19 20:20:27.290 32442-32567/com.example.guessthecelebrity W/System.err: at sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:125)
2021-08-19 20:20:27.290 32442-32567/com.example.guessthecelebrity W/System.err: ... 48 more
2021-08-19 20:20:27.290 32442-32567/com.example.guessthecelebrity I/Result: Failed in Background
2021-08-19 20:20:27.292 32442-32442/com.example.guessthecelebrity I/Choreographer: Skipped 325 frames! The application may be doing too much work on its main thread.
What did I do wrong? I have watched videos regarding this and they had the exact same code and it worked perfectly for them.