逝去的青春
android编程-解析URL-类同php的parse_url函数
2013-10-5 jao


我们知道,在php中有个parse_url函数可以解析URL,也很方便,但是,在写安卓程序时也需要一个类似的函数







因为安卓没有这个内置函数,每次都解析很麻烦,于是,我也就写了一个这样的函数







public static String[] parse_url(String url){
if(url==null){
return null;
}else{
String host = null;
if(url.substring(0, 7).equals("http://")){
url=url.substring(7);
}
if(url.indexOf("/") != -1){
host=url.substring(0, url.indexOf("/"));
}else{
host=url;
}
String port="80";
if(host.indexOf(":") != -1){
String[] X=host.split(":");
port=X[1];
host=X[0];

}
String path="";
if(url.indexOf("/") != -1){
String[] URL=url.split("/");
path="";
for(int i=1;i<URL.length;i++){
path+="/"+URL[i];
}

}

String[] arr=new String[3];
arr[0]=host;
arr[1]=port;
arr[2]=path;
return arr;
}

}

希望对大家有所帮助吧