Android post contains data request methods. The transmitted data formats include json, map, and androidjson.

Source: Internet
Author: User

Android post contains data request methods. The transmitted data formats include json, map, and androidjson.

As follows:

Public static String httpPost (String url, String json ){
Try {
URL u = new URL (url );
HttpURLConnection httpURLConnection = (HttpURLConnection) u. openConnection ();
HttpURLConnection. setConnectTimeout (TIMEOUT );
HttpURLConnection. setDoInput (true );
HttpURLConnection. setDoOutput (true );
HttpURLConnection. setRequestMethod ("POST ");
HttpURLConnection. setUseCaches (false );

HttpURLConnection. setRequestProperty ("Content-Type ",
"Application/json ");

HttpURLConnection. setRequestProperty ("Content-Length ",
String. valueOf (json. getBytes (). length ));

OutputStream outputStream = httpURLConnection. getOutputStream ();
OutputStream. write (json. getBytes ());

Int response = httpURLConnection. getResponseCode ();
If (response = HttpURLConnection. HTTP_ OK ){
InputStream inptStream = httpURLConnection. getInputStream ();
Return dealResponseResult (inptStream );
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return "";
}

Private static String dealResponseResult (InputStream inputStream ){
String resultData = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
Byte [] data = new byte [1024];
Int len = 0;
Try {
While (len = inputStream. read (data ))! =-1 ){
ByteArrayOutputStream. write (data, 0, len );
}
} Catch (IOException e ){
E. printStackTrace ();
}
ResultData = new String (byteArrayOutputStream. toByteArray ());
Return resultData;
}

If the transmitted value is not in json format, but map, you can use the following format:

Public static String httpPost (String url, Map <String, String> params ){
Byte [] data = getRequestData (params, "UTF-8"). toString (). getBytes ();
Try {
URL u = new URL (url );
HttpURLConnection httpURLConnection = (HttpURLConnection) u. openConnection ();
HttpURLConnection. setConnectTimeout (TIMEOUT );
HttpURLConnection. setDoInput (true );
HttpURLConnection. setDoOutput (true );
HttpURLConnection. setRequestMethod ("POST ");
HttpURLConnection. setUseCaches (false );

HttpURLConnection. setRequestProperty ("Content-Type ",
"Application/x-www-form-urlencoded ");

HttpURLConnection. setRequestProperty ("Content-Length ",
String. valueOf (data. length ));

OutputStream outputStream = httpURLConnection. getOutputStream ();
OutputStream. write (data );

Int response = httpURLConnection. getResponseCode ();
If (response = HttpURLConnection. HTTP_ OK ){
InputStream inptStream = httpURLConnection. getInputStream ();
Return dealResponseResult (inptStream );
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return "";
}

Private static StringBuffer getRequestData (Map <String, String> params,
String encode ){
StringBuffer stringBuffer = new StringBuffer ();
Try {
For (Map. Entry <String, String> entry: params. entrySet ()){
StringBuffer. append (entry. getKey ())
. Append ("= ")
. Append (URLEncoder. encode (entry. getValue (), encode ))
. Append ("&");
}
StringBuffer. deleteCharAt (stringBuffer. length ()-1); // remove the last "&"
} Catch (Exception e ){
E. printStackTrace ();
}
Return stringBuffer;
}

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.