Reputation: 521
I am trying to open up a webpage from an a button in Android but can't quite get it to work. I found a tutorial that seemed pretty straightforward but the trick is that I'm trying to display a url that was taken from an object created in another activity class. The code I'm using is a standard OnClickListener:
private void addListeneronButton() {
// TODO Auto-generated method stub
button1 = (Button) findViewById(R.id.stationHistory);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse(//need help here));
startActivity(browserIntent);
}
The project is reading a list of weather stations. I have a Station class with appropriate getters/setters so the Url is already stored. I just don't know the appropriate command to access from here. I thought it would be as easy as Uri.parse(station.get_url) but that then it just prompts me create local variable...Does anyone have any suggestions?
<station>
<station_id>NFNA</station_id>
<state>FJ</state>
<station_name>Nausori</station_name>
<latitude>-18.05</latitude>
<longitude>178.567</longitude>
<html_url>http://weather.noaa.gov/weather/current/NFNA.html</html_url>
<rss_url>http://weather.gov/xml/current_obs/NFNA.rss</rss_url>
<xml_url>http://weather.gov/xml/current_obs/NFNA.xml</xml_url>
</station>
<station>
<station_id>KCEW</station_id>
<state>FL</state>
<station_name>Crestview, Sikes Airport</station_name>
<latitude>30.79</latitude>
<longitude>-86.52</longitude>
<html_url>http://weather.noaa.gov/weather/current/KCEW.html</html_url>
<rss_url>http://weather.gov/xml/current_obs/KCEW.rss</rss_url>
<xml_url>http://weather.gov/xml/current_obs/KCEW.xml</xml_url>
</station>
<station>
<station_id>KDTS</station_id>
<state>FL</state>
<station_name>Destin, Ft. Walton Beach Airport</station_name>
<latitude>30.4</latitude>
<longitude>-86.47</longitude>
<html_url>http://weather.noaa.gov/weather/current/KDTS.html</html_url>
<rss_url>http://weather.gov/xml/current_obs/KDTS.rss</rss_url>
<xml_url>http://weather.gov/xml/current_obs/KDTS.xml</xml_url>
</station>
I'm parsing these into a Station Class with constructors/getters/setters. From a previous Activity, the user selects one of these stations. On the next activity it displays name, id, etc elements from this xml. But now I want to open the stations url from a button and I'm not sure how.
Upvotes: 0
Views: 2051
Reputation: 44571
I think I see what you are saying is the problem now, although you really didn't make it very clear. You have this
private void addListeneronButton() {
// TODO Auto-generated method stub
button1 = (Button) findViewById(R.id.stationHistory);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse(station.get_url);
startActivity(browserIntent);
}
and it is telling you that station.get_url
is not a variable so you need to create one. get_url
should be get_url()
if it is a method. If your class is Station
(capital "S") then you would access it with Station.get_url()
. However, you may need to pass in a certain parameter to tell it which url to get. This is assuming it is a static
method. If it is not then you need to create an instance of the Station
class and call the method
Station myStation = new Station(); //pass parameters here to constructor as needed
String address = myStation.get_url(); // object
Then you can use address
in your Intent
.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(address));
startActivity(browserIntent);
If http://
isn't passed back with the url then it will have to be appended before using it in the Intent
, as wangyif2 has already stated. I hope this makes sense but its the best I can do with the information given.
Upvotes: 2
Reputation: 2863
Try putting http:// before the string when you do your URI parsing:
private void addListeneronButton() {
button1 = (Button) findViewById(R.id.stationHistory);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(Station.getHtmlUrl("NFNA"));
startActivity(browserIntent);
}
}
Station class:
public static String getHtmlUrl(String station) {
//return the full URL here based on station
//eg. "http://weather.noaa.gov/weather/current/NFNA.html"
}
Upvotes: 1