【问题标题】:How to get and show content from url by pressing a button?如何通过按下按钮从 url 获取和显示内容?
【发布时间】:2012-07-09 00:05:21
【问题描述】:

如何通过在 android 中按下按钮从 url 获取和显示内容? 按钮在xml中,我需要在下面显示显示信息。

按钮 --> 来自http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=1的信息

我的错误代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bovalpo);

    Button ButtonOne = (Button)findViewById(R.id.btn);

    ButtonOne.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
          Intent viewIntent =

            new Intent("android.intent.action.VIEW",
            Uri.parse("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=1"));
            startActivity(viewIntent);
        }
    });

【问题讨论】:

  • 移除了不相关的 eclipse 标签

标签: android url button show


【解决方案1】:

你的意图是错误的。尝试使用:

Intent intent = new intent(Intent.ACTION_VIEW, Uri.parse("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=1"));
startActivity(intent);

【讨论】:

    【解决方案2】:

    如果您想在按下Button 的同一视图中显示来自链接的信息,请不要使用Intent,因为它会将您带到另一个视图。

    您可能想要使用AsyncTask (about),它将通过互联网在后台获取信息,然后将其显示在按钮下方或屏幕上的任何其他位置。

    要建立网络连接,请使用:

    url = new URL("http://...");
    urlConnection = url.openConnection();
    InputStream inputStream = urlConnection.getInputStream();
    

    要获取网络数据,请尝试this topic 中显示的代码。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 2012-07-24
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2019-04-13
      相关资源
      最近更新 更多