Android uses three methods to obtain webpages (submit forms through post and get)

Source: Internet
Author: User

Here, we have integrated the three types of information used to obtain the webpage content. We have already processed the preceding three methods for submitting and uploading files through a form. Now we have integrated these three methods into one, to help you make a comparison, the following are three methods:Code:

A total of three functions can be called directly, but remember to add access permissions when accessing the network.

 

Code

    //  Get information directly  
Void Directinfo () Throws Ioexception {

URL = New URL (SRC );

Httpurlconnection httpconn = (Httpurlconnection) URL. openconnection ();

Inputstreamreader instreamreader = New Inputstreamreader (httpconn
. Getinputstream ());

Bufferedreader bufreader = New Bufferedreader (instreamreader );

String line = "" ;
String date = " OK " ;
While (Line = Bufreader. Readline ()) ! = Null ){
Date + = Line + " \ N " ;
}

Edit1.settext (date );

}

// Get information
Void Getinfo () Throws Ioexception {
// Modify the preceding method directly.

URL = New URL (SRC + " /Default. aspx? Name = "
+ Urlencoder. encode ( " ABC " , " UTF-8 " ));
Httpurlconnection httpconn = (Httpurlconnection) URL. openconnection ();

Inputstreamreader inputreader = New Inputstreamreader (httpconn
. Getinputstream ());

Bufferedreader bufreader = New Bufferedreader (inputreader );

String line = "" ;
String date = "" ;

While (Line = Bufreader. Readline ()) ! = Null ){
Date + = Line;
}

Edit1.settext (date );

}

// POST method to obtain information
Void Postinfo () Throws Malformedurlexception, ioexception {
// The post method requires more parameters than the get method.

Httpurlconnection httpconn = (Httpurlconnection) New URL (SRC)
. Openconnection ();
// Post mode. The input and output parameters must be set to true.
Httpconn. setdoinput ( True );
Httpconn. setdooutput ( True );
Httpconn. setrequestmethod ( " Post " ); // Set to post mode. The default value is get.
Httpconn. setusecaches ( False ); // No Cache
Httpconn. setinstancefollowredirects ( True ); // Redirection
Httpconn. setrequestproperty ( " Content-Type " ,
" Application/X-WWW-form-urlencoded " ); // Set the connection Content-Type:
// Application/X-WWW-form-urlencoded
Httpconn. Connect (); // Connection

Dataoutputstream out = New Dataoutputstream (httpconn. getoutputstream ()); // Declare data write stream

String content = " Name = " + Urlencoder. encode ( " Fly_binbin " , " Gb2312 " );

Out. writebytes (content );

Out. Flush ();
Out. Close ();

Bufferedreader = New Bufferedreader ( New Inputstreamreader (httpconn. getinputstream ()));

String line = "" ;
String resultdate = "" ;
While (Line = Reader. Readline ()) ! = Null )
{
Resultdate + = Line;
}
Edit1.settext (resultdate );

}

 

You can create a test server by yourself. I wrote this test server myself and used it for testing. It was written in Asp.net and the results were the same in other methods. The results of using Web services are the same!

Related Article

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.