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