submit HTTP Post Data - Android Network

Android examples for Network:HTTP Post

Description

submit HTTP Post Data

Demo Code


//package com.java2s;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;
import java.io.InputStream;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import java.io.ByteArrayOutputStream;
import android.os.StrictMode;

public class Main {
    public static String submitPostData(String strUrlPath,
            Map<String, String> params, String encode) {

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());//from  ww  w  . ja v a  2  s . c o m
        byte[] data = getRequestData(params, encode).toString().getBytes();//?
        try {

            URL url = new URL(strUrlPath);

            HttpURLConnection httpURLConnection = (HttpURLConnection) url
                    .openConnection();
            httpURLConnection.setConnectTimeout(30000); //
            httpURLConnection.setDoInput(true); //
            httpURLConnection.setDoOutput(true); //
            httpURLConnection.setRequestMethod("POST"); //?Post?
            httpURLConnection.setUseCaches(false); //Post
            //?
            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 (IOException e) {
            //e.printStackTrace();
            return "err: " + e.getMessage().toString();
        }
        return "-1";
    }

    public 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); //?"&"
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer;
    }

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

Related Tutorials