Android post with data request method, the data format that is passed includes JSON and map

Source: Internet
Author: User

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 value passed is not in JSON format, but map can take 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;
}

Android post with data request method, the data format that is passed includes JSON and map

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.