好的,我不确定如何说出这个问题,但基本上我想要做的是,我从android中的RSS源获取了一个url,我需要将该url的一部分放入一个字符串中, url看起来像这样:http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821'
我只想要id = 仅ID ID 之后的部分,然后我需要id将其放在以下网址中:{{1} },在Glide中加载与id对应的图像: [已解决] 此部分已解决,但我还有其他问题
我必须这样做
第一种方式:
http://shake.uprm.edu/~shake/archive/shake/**ID HERE**/download/tvmap.jpg
第二种方式:
//the original String
String somestring = "http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821";
//save the index of the string '=' since after that is were you find your number, remember to add one as the begin index is inclusive
int beginIndex = somestring.indexOf("=") + 1;
//if the number ends the string then save the length of the string as the end, you can change this index if that's not the case
int endIndex = somestring.length();
//Obtain the substring using the indexes you obtained (if the number ends the string you can ignore the second index, but i leave it here so you may use it if that's not the case)
String theNumber = somestring.substring(beginIndex,endIndex);
//printing the number for testing purposes
System.out.println("The number is: " + theNumber);
//Then create a new string with the data you want (I recommend using StringBuilder) with the first part of what you want
StringBuilder sb=new StringBuilder("http://shake.uprm.edu/~shake/archive/shake/");
// add the number
sb.append(theNumber);
//then the rest of the string
sb.append("/download/tvmap.jpg");
//Saving the String in a variable
String endResult = sb.toString();
//Verifying end result
System.out.println("The end result is: "+endResult);
Glide.with(context).load(endResult).into(holder.Thumbnail);
[问题]
我的问题是,如果我正常放置网址,即String url = "http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821";
String[] array = url.split("id=");
String id = array[1];
String urlToLoad = "http://shake.uprm.edu/~shake/archive/shake/"+id+"/download/tvmap.jpg"
Glide.with(context).load(urlToLoad).into(holder.Thumbnail);
这两种方法对我有用,但如果我得到网址Via http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821
对我不起作用。 请帮帮我
我希望我解释得很好。
提前致谢。
我的 Myadapter.java 获取链接的方法是 getLink()
current.getLink()
答案 0 :(得分:0)
这是正则表达式的完美用例:
Matcher matcher = Pattern.compile("id=(\\d*)")
.matcher("http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821");
if(matcher.matches())
id = matcher.find().group();
在这里,id=(\\d*)
正则表达式是" find' id ='在一个字符串后面有任意数字的数字。"如果Matcher
能够找到它,则括号(组)允许我们提取您要查找的特定数据。
有关正则表达式的更多信息,请查看regex101
答案 1 :(得分:0)
尝试Uri.parse(url).getQueryParameter("id")
。解析函数返回一个URI对象,该URI将URI分解为多个部分,getQueryParameter查找嵌入的查询参数并返回字符串值。如果你需要int值,你可以正常方式转换它。