HTTP send Post - Android Network

Android examples for Network:HTTP Post

Description

HTTP send Post

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import android.util.Log;

public class Main {

    public static String sendPost(String url, String parmas) {
        String result = "";
        PrintWriter printWriter = null;
        BufferedReader bufferedReader = null;
        try {/*from  www.j a va  2s  .  c  om*/
            URL realUrl = new URL(url);
            try {
                URLConnection urlConnection = realUrl.openConnection();
                urlConnection.setRequestProperty("accept", "*/*");
                urlConnection
                        .setRequestProperty("connection", "Keep-Alive");
                urlConnection
                        .setRequestProperty("user-agent",
                                "Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1; SV1)");
                urlConnection.setDoOutput(true);
                urlConnection.setDoInput(true);
                Map<String, List<String>> map = urlConnection
                        .getHeaderFields();
                printWriter = new PrintWriter(
                        urlConnection.getOutputStream());
                printWriter.print(parmas);
                printWriter.flush();
                bufferedReader = new BufferedReader(new InputStreamReader(
                        urlConnection.getInputStream()));
                String line;
                for (; (line = bufferedReader.readLine()) != null;) {
                    result += "\n" + line;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } finally {
            if (null != bufferedReader) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != printWriter) {
                printWriter.close();
            }
        }
        return result;
    }
}

Related Tutorials