[問題] URI/URL檢查
假如有一個使用者輸入的字串結果為
String s1 = "http://www.google.com";
String s2 = "http://hello";
請問我要如何辨識 s1,s2 是否為正確的URI/URL
辨識結果應該為
s1->true
s2->false
查詢了跟URI相關的資料 都找不到能呈現我期望結果~"~
------------------------------------------------
找到一個解決方法 透過網路連線去做驗證 不過有沒有更簡單的做法 謝謝
首先在AndroidManifest.xml中開啓網路權限
<uses-permission android:name="android.permission.INTERNET" />
code部分
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpget;
String result = "false";
try{
httpget = new HttpGet(new URI(s));
HttpResponse resp = httpClient.execute(httpget);//開始連線
java.io.BufferedReader buf =
new java.io.BufferedReader(
new java.io.InputStreamReader(
resp.getEntity().getContent()));
if(buf != null)
ss = "true";
}catch(Exception ex){}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.170.0.1
→
05/03 17:06, , 1F
05/03 17:06, 1F
→
05/03 17:41, , 2F
05/03 17:41, 2F
URLUtil.isValidUrl(url) 是一個非常不準確地判斷檢查
"http://hello" ->true
"www.google.com"->false
感覺是單純的字串檢查 所以準確度很差 但我找到的解決方法是透過網路連線
會有cpu busy waiting的問題
※ 編輯: VVll 來自: 118.170.0.1 (05/04 12:53)
→
05/04 17:18, , 3F
05/04 17:18, 3F
→
05/07 12:26, , 4F
05/07 12:26, 4F
→
05/07 15:10, , 5F
05/07 15:10, 5F