Android Utililty Methods Http Post

List of utility methods to do Http Post

Description

The list of methods to do Http Post are organized into topic(s).

Method

StringhttpPost(String... params)
Makes a POST call to the server.
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(params[1]);
try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    for (int i = 1; i + 1 < params.length; i += 2) {
        nameValuePairs.add(new BasicNameValuePair(params[i],
                params[i + 1]));
    if (!nameValuePairs.isEmpty()) {
        request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    String credentials = "jsvgoncalves@gmail.com" + ":" + "123456";
    String base64EncodedCredentials = Base64.encodeToString(
            credentials.getBytes(), Base64.NO_WRAP);
    request.addHeader("Authorization", "Basic "
            + base64EncodedCredentials);
    HttpResponse response = httpclient.execute(request);
    System.out.println(response.toString());
    return response.toString();
} catch (ClientProtocolException e) {
    e.printStackTrace();
    return "Client Protocol Exception";
} catch (IOException e) {
    e.printStackTrace();
    return "POST: Bad Con";
voidwriteData(HttpURLConnection conn, String boundary, File file, String mimeType, String fieldName)
write Data
DataOutputStream requestData = new DataOutputStream(
        conn.getOutputStream());
requestData.writeBytes("--" + boundary + CRLF);
requestData.writeBytes("Content-Disposition: form-data; name=\""
        + fieldName + "\"; filename=\"" + file.getName() + "\""
        + CRLF);
requestData.writeBytes("Content-Type: " + mimeType + CRLF + CRLF);
InputStream fileInput = new FileInputStream(file);
...
JSONObjectprepareAndSendHttpPost(String URI, ArrayList params)
prepare And Send Http Post
JSONObject json = null;
try {
    json = new JSONObject();
    if (params != null) {
        Iterator<NameValuePair> it = params.iterator();
        while (it.hasNext()) {
            NameValuePair pair = it.next();
            json.put(pair.getName().toString(), pair.getValue());
...
ListbuildNameValuePair( Hashtable httpPost)
build Name Value Pair
if (httpPost == null)
    return null;
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
Enumeration<String> keys = httpPost.keys();
while (keys.hasMoreElements()) {
    String key = (String) keys.nextElement();
    String value = (String) httpPost.get(key);
    BasicNameValuePair nv = new BasicNameValuePair(key, value);
...
InputStreamsendPostRequest(String path, String params, String encoding)
send Post Request
byte[] data = params.getBytes(encoding);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",
        "application/x-javascript; charset=" + encoding);
conn.setRequestProperty("Content-Length",
...
StringexecutePost(String url, String parameters)
execute Post
URL request = new URL(url);
HttpURLConnection connection = (HttpURLConnection) request
        .openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("User-Agent", "StripeConnectAndroid");
...