• 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
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
 
Welcome to Android Forums cjava. I moved your thread here to the Application Development forum to get some better eyes on it.:)
 
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. :)
 
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?
 
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.
 
Back
Top Bottom