Android HTTP protocol (I)

Source: Internet
Author: User

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

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.