Executes an HTTP POST where a string is returned from the network - Android Network

Android examples for Network:HTTP Post

Description

Executes an HTTP POST where a string is returned from the network

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class Main {
    private final static String sTag = "HttpUtils";
    private static int sTimeout = 10000;

    /**/*from   w w  w.  ja  va2s  .  co m*/
     * Executes an HTTP POST where a string is returned from the network
     * 
     * @param url
     *            to be accessed
     * @param content
     *            data to send to the remote server
     * 
     * @return data returned from the url
     */
    public static String executeHttpPost(String url, String content) {
        final StringBuilder retVal = new StringBuilder();
        BufferedReader bReader = null;
        InputStreamReader iReader = null;
        HttpURLConnection connection = null;
        try {
            // send the data to the remote server
            byte[] contentBytes = content.getBytes();
            connection = getUrlConnection(url);
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("Content-Length",
                    Integer.toString(contentBytes.length));
            DataOutputStream output = new DataOutputStream(
                    connection.getOutputStream());
            output.write(contentBytes);
            output.flush();
            output.close();
            // read the response from the server
            bReader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = bReader.readLine()) != null) {
                retVal.append(line);
            }
            Log.v(sTag, "Content Size: " + retVal.length());
            Log.v(sTag, "Response Code: " + connection.getResponseCode());
        } catch (IOException e) {
            Log.w(sTag, e.getMessage());
            return null;
        } finally {
            try {
                if (iReader != null)
                    iReader.close();
                if (bReader != null)
                    bReader.close();
                connection.disconnect();
            } catch (Exception e) {
                Log.w(sTag, e.getMessage());
            }
        }

        return retVal.toString();
    }

    /**
     * Returns a URL connection object tailor made for the Google APIs
     * 
     * @param urlString
     *            URL to connect to
     * @return
     * @throws IOException
     */
    public static HttpURLConnection getUrlConnection(String urlString)
            throws IOException {
        Log.v(sTag, "New URL connection: " + urlString);
        URL url = new URL(urlString);
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        urlConnection.setReadTimeout(sTimeout);
        return urlConnection;
    }
}

Related Tutorials