Created
February 23, 2017 16:57
-
-
Save r-trigo/684f99b7fb5be74bf2c7e54d81d8c7ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Sending a POST request | |
public int SendThisByPOST(URL myURL, JSONObject params) { | |
HttpURLConnection conn = null; | |
DataOutputStream output; | |
int response = 0; | |
try { | |
conn = (HttpURLConnection) myURL.openConnection(); | |
conn.setRequestMethod("POST"); | |
conn.setDoOutput(true); | |
output = new DataOutputStream(conn.getOutputStream()); | |
//Overperformance choices | |
//conn.setFixedLengthStreamingMode(output.size()); | |
//conn.setChunkedStreamingMode(0); | |
byte[] outputInBytes = params.toString().getBytes("UTF-8"); | |
output.write(outputInBytes); | |
output.flush(); | |
output.close(); | |
//Get server response | |
response = conn.getResponseCode(); | |
//DEBUG - Get the server response | |
String reply; | |
InputStream in = conn.getErrorStream(); | |
StringBuffer sb = new StringBuffer(); | |
int chr; | |
if (in != null) { | |
while ((chr = in.read()) != -1) { | |
sb.append((char) chr); | |
} | |
reply = sb.toString(); | |
Log.d("reply", reply); | |
} | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
if (conn != null) { | |
conn.disconnect(); | |
} | |
} | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment