Java 从OnClickListener Android打开Url
我试图打开一个网页,从一个按钮在安卓系统,但不能完全让它工作。我发现了一个看起来非常简单的教程,但诀窍在于我试图显示一个url,该url取自另一个activity类中创建的对象。我使用的代码是标准的OnClick侦听器:Java 从OnClickListener Android打开Url,java,android,url,webview,onclicklistener,Java,Android,Url,Webview,Onclicklistener,我试图打开一个网页,从一个按钮在安卓系统,但不能完全让它工作。我发现了一个看起来非常简单的教程,但诀窍在于我试图显示一个url,该url取自另一个activity类中创建的对象。我使用的代码是标准的OnClick侦听器: private void addListeneronButton() { // TODO Auto-generated method stub button1 = (Button) findViewById(R.id.stationHistory);
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);
}
该项目正在阅读气象站的列表。我有一个带有适当getter/setter的Station类,因此Url已经存储。我只是不知道从这里访问的合适命令。我原以为这和Uri.parse(station.get_url)一样简单,但它只会提示我创建局部变量……有人有什么建议吗
<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>
NFNA
FJ
诺索里
-18.05
178.567
http://weather.noaa.gov/weather/current/NFNA.html
http://weather.gov/xml/current_obs/NFNA.rss
http://weather.gov/xml/current_obs/NFNA.xml
KCEW
佛罗里达州
克雷斯特维尤,赛克斯机场
30.79
-86.52
http://weather.noaa.gov/weather/current/KCEW.html
http://weather.gov/xml/current_obs/KCEW.rss
http://weather.gov/xml/current_obs/KCEW.xml
KDTS
佛罗里达州
沃尔顿海滩机场
30.4
-86.47
http://weather.noaa.gov/weather/current/KDTS.html
http://weather.gov/xml/current_obs/KDTS.rss
http://weather.gov/xml/current_obs/KDTS.xml
我正在用构造函数/getter/setter将它们解析为Station类。从上一个活动中,用户选择其中一个电台。在下一个活动中,它将显示此xml中的name、id等元素。但是现在我想从按钮打开站点url,我不确定如何打开。在进行URI解析时,尝试将http://放在字符串前面:
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);
}
}
车站等级:
public static String getHtmlUrl(String station) {
//return the full URL here based on station
//eg. "http://weather.noaa.gov/weather/current/NFNA.html"
}
在执行URI解析时,请尝试将http://置于字符串之前:
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);
}
}
车站等级:
public static String getHtmlUrl(String station) {
//return the full URL here based on station
//eg. "http://weather.noaa.gov/weather/current/NFNA.html"
}
我想我明白你说的是现在的问题,尽管你真的没有说清楚。你有这个
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);
}
它告诉您,station.get\u url
不是一个变量,所以您需要创建一个变量<如果是方法,则code>get\u url应该是get\u url()
。如果您的类是Station
(大写“S”),那么您可以使用Station.get\u url()
访问它。但是,您可能需要传入某个参数来告诉它要获取哪个url。这是假设它是一个静态方法。如果不是,则需要创建站
类的实例并调用该方法
Station myStation = new Station(); //pass parameters here to constructor as needed
String address = myStation.get_url(); // object
然后您可以在您的意图中使用地址
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(address));
startActivity(browserIntent);
如果http://
没有与url一起传回,那么在Intent
中使用它之前,它必须被追加,正如wangyif2所述。我希望这是有道理的,但这是我对所给信息所能做的最好的了。我想我知道你说的是现在的问题,尽管你确实没有说得很清楚。你有这个
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);
}
它告诉您,station.get\u url
不是一个变量,所以您需要创建一个变量<如果是方法,则code>get\u url
应该是get\u url()
。如果您的类是Station
(大写“S”),那么您可以使用Station.get\u url()
访问它。但是,您可能需要传入某个参数来告诉它要获取哪个url。这是假设它是一个静态方法。如果不是,则需要创建站
类的实例并调用该方法
Station myStation = new Station(); //pass parameters here to constructor as needed
String address = myStation.get_url(); // object
然后您可以在您的意图中使用地址
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(address));
startActivity(browserIntent);
如果http://
没有与url一起传回,那么在Intent
中使用它之前,它必须被追加,正如wangyif2所述。我希望这是有意义的,但这是我所能做的最好的信息。粘贴URL,请注意,您需要在Uri中包含http://
或https://
。解析示例url应该是:所以请尝试将http://
放在前面:http://http://weather.noaa.gov/weather/current/NFNA.html
但我是从上面发布的一个示例中获得这些信息的。我不知道为什么这样做不起作用,但您试过了吗创建一个变量,如Uri=http://weather.noaa.gov/weather/current/NFNA.html
,然后将其放入您的Intent
?粘贴URL,请注意,您需要在Uri中包含http://
或https://
。解析示例url应该是:所以请尝试将http://
放在前面:http://http://weather.noaa.gov/weather/current/NFNA.html
但我是从上面发布的一个示例中获得这些信息的。我不知道为什么这样做不起作用,但您试过了吗创建一个变量,比如uriuri=http://weather.noaa.gov/weather/current/NFNA.html
,然后将其放入您的Intent
?再次检查,这是您想要的吗?再次检查,这是您想要的吗?