這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在Android中對(duì)url地址進(jìn)行檢測(cè),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
方法一
try{ URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setUseCaches(false); conn.setInstanceFollowRedirects(true); conn.setConnectTimeout(waitMilliSecond); conn.setReadTimeout(waitMilliSecond); //HTTP connect try { conn.connect(); } catch(Exception e) { e.printStackTrace(); return false; } int code = conn.getResponseCode(); if ((code >= 100) && (code < 400)){ return true; } return false; }catch (Exception e){ e.printStackTrace(); return false; }
方法二
try { URL url = new URL(address); String host = url.getHost(); int port = url.getPort(); if (port == -1) { port = 80; } Socket socket = new Socket(); InetSocketAddress isa = new InetSocketAddress(InetAddress.getByName(host), port); socket.connect(isa, timeout); if (socket.isConnected()) { return true; } else { return false; } } catch (Exception e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } }Android是什么
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
上述就是小編為大家分享的怎么在Android中對(duì)url地址進(jìn)行檢測(cè)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)標(biāo)題:怎么在Android中對(duì)url地址進(jìn)行檢測(cè)-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)源:http://aaarwkj.com/article46/ccjphg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、企業(yè)網(wǎng)站制作、網(wǎng)站營(yíng)銷、網(wǎng)站改版、靜態(tài)網(wǎng)站、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容