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;
}