Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to URL in android/java [duplicate]

I have a string. i.e.

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:

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.

like image 361
William He Avatar asked Dec 06 '16 20:12

William He


People also ask

How to make String as url in android?

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

How can I convert String to URL in Java?

In your Java program, you can use a String containing this text to create a URL object: URL myURL = new URL("http://example.com/"); The URL object created above represents an absolute URL. An absolute URL contains all of the information necessary to reach the resource in question.

How do I turn a String into a URL?

You should first convert your string into a URI , then convert the URI into a URL . For example: String str = "http://google.com"; URI uri = new URI(str); URL url = uri. toURL();


2 Answers

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

String urlString = "http://m.nasdaq.com/symbol/"+stock_symbol.getText();
URL myURL = new URL(urlString);
like image 107
NineBerry Avatar answered Sep 23 '22 14:09

NineBerry


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

like image 34
Shahbazi Avatar answered Sep 26 '22 14:09

Shahbazi