Android URL Download sendPOST(final String myurl, final String paramns)

Here you can find the source of sendPOST(final String myurl, final String paramns)

Description

send POST

License

Open Source License

Declaration

public static String sendPOST(final String myurl, final String paramns)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.IOException;

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

public class Main {
    private static final String URL = "http://thiner.herokuapp.com/api/user";

    public static String sendPOST(final String myurl, final String paramns)
            throws IOException {

        final URL url = new URL(myurl);
        final HttpURLConnection conn = (HttpURLConnection) url
                .openConnection();//ww  w  . java2 s  . co m
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        final OutputStreamWriter osw = new OutputStreamWriter(
                conn.getOutputStream(), "UTF-8");
        osw.write(paramns);
        osw.flush();

        final BufferedReader br = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), "UTF-8"));

        final StringBuffer contentAsString = new StringBuffer();
        String line;

        while ((line = br.readLine()) != null) {
            contentAsString.append(line);
        }

        osw.close();
        br.close();

        return contentAsString.toString();
    }
}

Related

  1. getJSONResponseFromURL(String url, Hashtable httpGetParams)
  2. downloadJson(URI uri)
  3. getURLString(String urlStr)
  4. getBytesFromUrl(String url)
  5. getWebContent(String pvUrl)
  6. connectForMultipart(String url)