我正在制作一个大型应用程序,我有点卡住(实际上现在没时间抓住我的脑袋)。这是我的问题。
我在Glide中以原始数组加载一堆URL图像(实际上它是一个字符串,所以我不确定它是否是原始的),但它的String[] array = {};
这些链接显示在gridview中的活动内部(不太重要)。 我想要的是检查链接是否有效。如果将阵列成员踢出去是无效的。
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; }
问题,程序无法访问这些链接。我得到exception.getMessage()
- &gt;
x20,因为有20个链接。什么可以作为线索,所有链接都作为String通过sharedPreferences加载到数组中。 值得一提的是,如果链接没有通过这种方法直接进入数组,链接工作正常。所有图片都很好。我的目标是在一段时间后启动过期的链接。 希望我明白自己。任何人都有任何线索,为什么链接不能使用这种方法?01-29 18:44:17.799 5705-5705 / com.mario.restaurantcroatia I / System.out:(HTTPLog)-Static:isSBSettingEnabled false
答案 0 :(得分:0)
我想要的是检查链接是否有效。如果将阵列成员踢出去是无效的。
如果我理解你的问题,作为一种简单的方法,你可以使用InetAddress类。
boolean reachable = InetAddress.isReachable();
遍历您的数组,然后执行相应的操作,以便在地址无响应时从数组中删除URL。
https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable(int)
答案 1 :(得分:0)
我已经解决了这个问题。
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; }