javaandroidhttpurlconnection

The line code connection.connect(); not wokring on java android


i want to download a picture from a website . I tried the code from here : https://developer.android.com/reference/java/net/HttpURLConnection. When debeuging : the third line works and i have a confirmation message, but the problem is that the fourth line code : " connection.connect()" never works and i don't have the confirmation message after it , and no error message also .. I don't know why .

Thank's for any help

URL url = new URL(src);                     
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);

Solution

  • I finally found it was because of StrictMode AndroidBlockGuardPolicy.onNetwork on verbose log cat . and the solution was by adding the 2 lines of code after the oncreate : StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);