-2

I am making a large app, and I am kind of stuck (actually have no time right now to grasp my head over it). This is my problem.

I am loading bunch of URL images over Glide in a primitive array (actually it's a String so I'm not sure if it's primitive or not), but its String[] array = {};

These links are shown inside of activity in gridview (less important). What I want is to check if link is valid. If it's not valid to kick an array member out.

public String[] checkingLinkValidation(String[] array) {

   ArrayList<String> newArrayList = new ArrayList<>( );
   String[] newArray = new String[newArrayList.size( )];
   for (String s : array) {
       if (Patterns.WEB_URL.matcher( s ).matches( ) && !s.equals( "" )) {
           if(URLUtil.isValidUrl( s )){
               newArrayList.add( s );
           }
       }

       /**
        * Nastaviti raditi na ovome. moram izbaciti link koji ne valja
        */
   try{
           HttpURLConnection httpURLConnection = (HttpURLConnection) new URL( s ).openConnection();
           httpURLConnection.setRequestMethod( "HEAD" );
           httpURLConnection.setConnectTimeout( 2000 );
           httpURLConnection.setReadTimeout( 2000 );

           if(httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
               newArrayList.add( s );
           }
       }catch (Exception e){
           Log.d( "HTTPURLConnection check if exists", "Error type: "+e.getMessage() );
       }


       newArray = newArrayList.toArray( newArray );


   }

   return newArray;  }

Problem is, program is not able to reach these links. I get exception.getMessage() ->

01-29 18:44:17.799 5705-5705/com.mario.restaurantcroatia I/System.out: (HTTPLog)-Static: isSBSettingEnabled false

x20, because there are 20 links. What could come as a clue is, all there links are loaded into an array as String over a sharedPreferences. It's also worth to mention, links are working just fine if it does not go over this method and they go directly into an array. All pictures are fine. My goal is to kick out expired links after some time.

Hope I made myself clear. Anyone have any clue why the links can't go thrue this method?

  • Just FYI since you touched on it in your question...Strings are not primitives. They are immutable objects. Refer to [this post](https://stackoverflow.com/questions/14473488/is-string-a-primitive-or-an-object-in-android-or-java) – jseashell Jan 29 '18 at 18:40
  • I do remember Tim BUtchalka teaching that Strings are actual class. Was not sure, but I do remember they are something different from primities. Ty – Obračun Plaće Jan 29 '18 at 19:54

2 Answers2

0

What I want is to check if link is valid. If it's not valid to kick an array member out.

If I understand your question correctly, as a simple approach you could use the InetAddress class.

boolean reachable = InetAddress.isReachable();

Iterate through your array and then do the appropriate operations to remove the URL's from the array if the address is unresponsive.

https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable(int)

Lord Why
  • 14
  • 6
0

I have solved the problem.

public String[] checkingLinkValidation(String[] array)  {

   ArrayList<String> newArrayList = new ArrayList<>( );
   String[] newArray = new String[newArrayList.size( )];
   for (String s : array) {
        if(URLUtil.isValidUrl( s )){

            newArrayList.add( s );
        }

       newArray = newArrayList.toArray( newArray );
   }

   return newArray;  }