• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Retrieve web page

cjava

Lurker
Mar 28, 2014
2
0
Greetings all. I'm looking for some help to get me started on what should be a simple task. I would like to be able to retrieve a web page or text from a web page to then parse and display on my device. A simple example might be retrieving data from a weather. I've been having trouble putting it together. Does anybody have any simple code or suggestions on retrieving a web page?
Thanks,
Craig
 
Have you looked at the WebView? That will allow you to look at the webstuff in an integrated "browser" in your app, so I believe.
If you wanted to open just a page, for example, in a seperate browser, you could do something like the following in a routine:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.gerbyte.com"));
startActivity(browserIntent);

public void postData(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.gerbyte.com/insertdata.php");
try {
// create a list to store HTTP variables and their values (POST values)
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("name", params[0]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);

InputStream is = response.getEntity().getContent(); // this is what will be returned from above insertdata.php file

//To make sure you store/print/whatever from the input stream, use the following...

BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s="";
String line="";
while ((line = rd.readLine()) != null) { s += line; }

} catch (ClientProtocolException e) {
// process execption
} catch (IOException e) {
// process execption
}
}

This should help. :)
 
  • Like
Reactions: Unforgiven
Upvote 0
Thank you for the responses however I should have been more complete with my original post. I would like to retrieve a web page and parse specific text from it on a daily basis, and display that text in my app, in a widget or possibly on the status bar at the top of the screen. Does anybody have any examples for this?
 
Upvote 0
Example above :
/To make sure you store/print/whatever from the input stream, use the following...

BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s="";
String line="";
while ((line = rd.readLine()) != null) { s += line; }


s becomes whatever the web page is. This could be html, pure text etc.
You will deal with s as you would with a normal string.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones