Android HTTP network data transmission memo

Source: Internet
Author: User
Import java. Io. bufferedinputstream;
Import java. Io. bufferedoutputstream;
Import java. Io. bytearrayoutputstream;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import java.net. httpurlconnection;
Import java.net. url;
Import java. Text. simpledateformat;
Import java. util. date;
Import java. util. Map;

Public class httptool {
Private Static int time_out = 1000*6;
Private Static string post = "Post ";
Private Static string get = "get ";
Private Static string utf8 = "UTF-8 ";
Public static void downloadget (string path) throws exception {
URL url = new URL (PATH );
Httpurlconnection connection = (httpurlconnection) URL. openconnection ();
Connection. setreadtimeout (time_out );
Connection. setrequestmethod (get );
Connection. setusecaches (false );
Connection. Connect ();
If (connection. getresponsecode ()! = 200 ){
Throw new runtimeexception ("responsecode! = 200 ");
}
Inputstream = connection. getinputstream ();
File file = new file (Rename (PATH ));
Fileoutputstream = new fileoutputstream (File );
Readinput (inputstream, fileoutputstream );
Connection. Disconnect ();
System. Out. println ("OK ");
}
Public static void downloadpost (string path, Map <string, string> Params) throws exception {
URL url = new URL (PATH );
Httpurlconnection connection = (httpurlconnection) URL. openconnection ();
Connection. setreadtimeout (time_out );
Connection. setrequestmethod (post );
Connection. setusecaches (false );
Connection. setdoinput (true );
Connection. setdooutput (true );
Connection. setrequestproperty ("Accept-charset", utf8 );
Connection. setrequestproperty ("connection", "keep-alive ");
Connection. setrequestproperty ("Content-Type", "application/X-WWW-form-urlencoded; charset =" + utf8 );
Stringbuilder sb = new stringbuilder ();
If (Params! = NULL ){
For (Map. Entry <string, string> item: Params. entryset ()){
SB. append (item. getkey () + "=" + item. getvalue (). append ("&");
}
SB = sb. deletecharat (sb. Length ()-1 );
Byte [] DATA = sb. tostring (). getbytes ();
Connection. setrequestproperty ("Content-Length", String. valueof (data. Length ));
Outputstream = connection. getoutputstream ();
Outputstream. Write (data );
Outputstream. Flush ();
Outputstream. Close ();
}
If (connection. getresponsecode ()! = 200 ){
Throw new runtimeexception ("responsecode! = 200 ");
}
Inputstream = connection. getinputstream ();
Bufferedinputstream = new bufferedinputstream (inputstream );
Byte [] buffer = new byte [1024];
Int Len =-1;
Bytearrayoutputstream arrayoutputstream = new bytearrayoutputstream ();
While (LEN = bufferedinputstream. Read (buffer ))! =-1 ){
Arrayoutputstream. Write (buffer, 0, Len );
}
Byte [] bytes = arrayoutputstream. tobytearray ();
String STR = new string (bytes, utf8 );
Arrayoutputstream. Close ();
Bufferedinputstream. Close ();
Connection. Disconnect ();
System. Out. println (STR );
}

/**
* Read the input stream
* @ Param inputstream
* @ Param fileoutputstream
* @ Throws exception
*/
Private Static void readinput (inputstream, fileoutputstream) throws exception {
Bufferedinputstream = new bufferedinputstream (inputstream );
Bufferedoutputstream = new bufferedoutputstream (fileoutputstream );
Byte [] buffer = new byte [1024];
Int Len =-1;
While (LEN = bufferedinputstream. Read (buffer ))! =-1 ){
Bufferedoutputstream. Write (buffer, 0, Len );
}
Bufferedoutputstream. Close ();
Bufferedinputstream. Close ();
}
/**
* Duplicate name
* @ Param path
* @ Return
*/
Public static string Rename (string path ){
String STR = path. substring (path. lastindexof ("."));
Return new simpledateformat ("yyyymmddhhmmsss"). Format (new date () + STR;
}
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.