Android can also open remote web pages and use httpclient
Get the page content. The following code is available in both methods:
Import java. Io. bufferedinputstream;
Import java. Io. bufferedreader;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java.net. url;
Import java.net. urlconnection;
Import org. Apache. http. httpentity;
Import org. Apache. http. httpresponse;
Import org. Apache. http. Client. Methods. httpget;
Import org. Apache. http. Client. Methods. httpurirequest;
Import org. Apache. http. impl. Client. defaulthttpclient;
Import org. Apache. http. util. bytearraybuffer;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview;
Public class androidtelenet extends activity {
/** Called when the activity is first created .*/
Httpurirequest request = NULL;
Httpresponse resp = NULL;
Inputstream is = NULL;
Defaulthttpclient client = new defaulthttpclient ();
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
/* We will show the data we read in a textview .*/
Textview TV = new textview (this );
/* Will be filled and displayed later .*/
Stringbuffer mystring = new stringbuffer ();
String tips = NULL;
Try {
/* Define the URL we want to load data from .*/
// URL myurl = new URL (
// "Http: // 10.0.2.2: 8085/test/index. Action ");
/* Open a connection to that URL .*/
// Urlconnection ucon = myurl. openconnection ();
/*
* Define inputstreams to read from the urlconnection.
*/
// Inputstream is = ucon. getinputstream ();
// Bufferedinputstream Bis = new bufferedinputstream (is );
// If (bis. Read ()> 0)
//{
// Tips = "connecting to webpages ";
//}
// Else
//{
// Tips = "cannot connect to webpage ";
//}
/*
* Read bytes to the buffer until there is nothing more to read (-1 ).
*/
// Bytearraybuffer BAF = new bytearraybuffer (50 );
// Int current = 0;
// While (current = bis. Read ())! =-1 ){
// BAF. append (byte) Current );
//}
/* Convert the bytes read to a string .*/
// Mystring = new string (BAF. tobytearray ());
Request = new httpget ("http: // 10.0.2.2: 8085/test/index. Action ");
Resp=client.exe cute (request );
Httpentity entity = resp. getentity ();
Inputstream is = entity. getcontent ();
Bufferedreader BR = new bufferedreader (
New inputstreamreader (is, "gb2312 "));
String data = "";
While (Data = Br. Readline ())! = NULL ){
Mystring. append (data );
}
String result = mystring. tostring ();
System. Out. println ("result is" + result );
System. Out. println ("mystring" + mystring );
If (mystring. indexof ("sorry, Operation error or system maintenance")> 0)
{
Tips = "Tomcat startup, database, and other exceptions ";
}
Else
{
Tips = "normal, no exception information ";
}
} Catch (exception e ){
/* On any error we want to display it .*/
Tips = E. getmessage ();
}
/* Show the string on the GUI .*/
TV. settext (TIPS );
This. setcontentview (TV );
}
}