Android examples for Network:URL
http Url Post from URL and parameter map
//package com.java2s; import android.util.Log; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Map; import java.util.Set; public class Main { public static String httpUrlPost(String URL, Map<String, Object> map) { String result = ""; try {/* ww w . j a v a 2 s .co m*/ // url URL url = new URL(URL); // url HttpURLConnection conn = (HttpURLConnection) url .openConnection(); // post conn.setRequestMethod("POST"); // conn.setDoOutput(true); conn.setDoInput(true); // conn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Charset", "utf-8"); // String data = checkMap(map); // conn.setRequestProperty("Content-Length", String.valueOf(data.getBytes().length)); // OutputStream out = conn.getOutputStream(); out.write(data.getBytes()); out.flush(); // InputStream in = conn.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(in)); // if (conn.getResponseCode() == 200) { String line = ""; while ((line = reader.readLine()) != null) { // result += line; } } else {// Log.i("info", "HttpUrlConnectionPost"); result = ""; } // in.close(); // conn.disconnect(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } private static String checkMap(Map<String, Object> map) { String data = ""; Set<String> set = map.keySet(); for (String key : set) { data = data + key + "=" + map.get(key) + "&"; } data = data.substring(0, data.length() - 1); return data; } }