Previously, we developed mobile phone OS in our company. The most painful thing is that our system does not have a ready-made HTTP protocol. We only use libcurl of the open-source library for encapsulation, in this process, I am very familiar with some HTTP request details. Now I think it's a good experience. Now I 've switched to Android. for Google, if Internet connection is hard to handle, the best internet company is too bad.
Example of using urlconnection
Download the HTML file and place it in the text control.
Public Class Androidnetactivity
Extends Activity {
Handler handler;
/** Called when the activity is first created. */
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Final Edittext etext = (edittext) findviewbyid (R. Id. Address );
Final Textview tview = (textview) findviewbyid (R. Id. pagetext );
Handler =
New Handler (){
Public Void Handlemessage (Message MSG ){
Switch (Msg. What)
{
Case 0:
{
Tview. append (string) msg. OBJ );
// Tview. settext (string) msg. OBJ );
Break ;
}
}
Super . Handlemessage (MSG );
}
};
Final Button button = (button) findviewbyid (R. Id. buttongo );
Button. setonclicklistener (
New Button. onclicklistener (){
@ Override
Public Void Onclick (view v ){
// Todo auto-generated method stub
Try {
Tview. settext ("");
URL url = New URL (etext. gettext (). tostring ());
Urlconnection conn = URL. openconnection ();
// // Method 1
Bufferedreader RD = New Bufferedreader (
New Inputstreamreader (conn. getinputstream ()));
String line = "";
While (Line = RD. Readline ())! = Null )
{
Message lmsg;
Lmsg = New Message ();
Lmsg. What = 0;
Lmsg. OBJ = line;
Androidnetactivity. This . Handler. sendmessage (lmsg );
}
// Method 2
// Inputstream is = NULL;
// Is = conn. getinputstream ();
// Byte [] result = NULL;
//
// Bytearrayoutputstream Bos = new bytearrayoutputstream ();
// While (true)
// {
// Byte [] bytes = new byte [64];
// Int TMP = is. Read (bytes );
// If (TMP =-1)
// {
// Break;
// }
// Else
// {
// Bos. Write (bytes, 0, TMP );
// }
// }
// Result = Bos. tobytearray ();
// String all;
// All = new string (result );
//
// Message MSG;
// MSG = new message ();
// MSG. What = 0;
// MSG. OBJ = all;
// Androidnetactivity. This. handler. sendmessage (MSG );
}
Catch (Exception E)
{
E. printstacktrace ();
}
}
});
}
}
Httpurlconnection
Inherited from urlconnection, which is equivalent to the encapsulated HTTP protocol. ExampleCode
Httpurlconnection conn =
Null ;
URL;
Conn = (httpurlconnection) URL. openconnection ();
Conn. setdoinput (
True );
Conn. setdooutput (
True );
Conn. setrequestmethod ("get ");
Conn. setrequestproperty ("accept", "application/octet-stream ,*/*");
Conn. setrequestproperty ("connection", "keep-alive ");
Conn. setrequestproperty ("Content-Type", "application/octet-stream ");
Conn. Connect ();
Int Code = conn. getresponsecode ();
Int Len = conn. getcontentlength ();
If (Conn = Null )
{
Return Null ;
}
Inputstream is = Null ;
Byte [] Result = Null ;
Bytearrayoutputstream Bos = New Bytearrayoutputstream ();
Is = conn. getinputstream ();
While ( True ){
Byte [] Bytes =New Byte [64];
Int TMP = is. Read (bytes );
If (TMP =-1)
{
Break ;
}
Else
{
Bos. Write (bytes, 0, TMP );
}
}
Result = Bos. tobytearray ();
Next time, sort out the httpclient