在android/java中将String转换为URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41004236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:04:14  来源:igfitidea点击:

Convert String to URL in android/java

javaandroidparsing

提问by William He

I have a string. i.e.

我有一个字符串。IE

String urlString = "http://m.nasdaq.com/symbol/"+stock_symbol.getText();

I need to keep it a string because I'm asking the user for the stock symbol then performing the search. I then need to parse the information that is in the html code. So I need to convert my urlString into an URL. I looked on the android developer and saw this:

我需要将它保留为一个字符串,因为我要求用户输入股票代码然后执行搜索。然后我需要解析 html 代码中的信息。所以我需要将我的 urlString 转换为 URL。我查看了 android 开发人员并看到了这一点:

URL(String spec) 
  Creates a URL object from the String representation.

but this isn't working. Need help I also need help parsing the actual html. You don't need to give me everything, just a very short example would be great.

但这不起作用。需要帮助我还需要帮助解析实际的 html。你不需要给我所有的东西,一个非常简短的例子就很好了。

回答by NineBerry

URLis a class. Simply use newto create a new instance of the class and pass the string as a parameter to the constructor.

URL是一个类。只需用于new创建类的新实例并将字符串作为参数传递给构造函数。

String urlString = "http://m.nasdaq.com/symbol/"+stock_symbol.getText();
URL myURL = new URL(urlString);

回答by Shahbazi

you most create a new Url and input your string in C URL url=new URL(urlString);

你最常创建一个新的 URL 并在 C URL 中输入你的字符串 url=new URL(urlString);