How to get and show content from url by pressing a button?

How can I get and show content from url by pressing a button in android? The button is in xml, and I need to show display information below.

Button --> information from http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=1

My wrong code:

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);
        }
    });

Upvotes: 0

Views: 411

Answers (2)

cyborg86pl
cyborg86pl

Reputation: 2617

If you want to present information from link in the same view in which you press the Button, then don't use Intent, because it gets you to another view.

What you might want to use is AsyncTask (about) that would get informations via internet in background, and then present it below button, or anywhere else on screen.

To make a web connection use:

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

And to get web data, try code shown in this topic.

Hope this helps.

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82563

Your Intent is wrong. Try using:

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

Upvotes: 1

Related Questions