Java tutorial
//package com.java2s; //License from project: Apache License import android.util.Log; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; public class Main { public static String post(String url, Map<String, String> params) { try { URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); PrintWriter pw = new PrintWriter(connection.getOutputStream()); StringBuilder sbParams = new StringBuilder(); if (params != null) { for (String key : params.keySet()) { sbParams.append(key + "=" + params.get(key) + "&"); } } if (sbParams.length() > 0) { String strParams = sbParams.substring(0, sbParams.length() - 1); Log.e("cat", "strParams:" + strParams); pw.write(strParams); pw.flush(); pw.close(); } BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuffer response = new StringBuffer(); String readLine = ""; while ((readLine = br.readLine()) != null) { response.append(readLine); } br.close(); return response.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } public static String get(String url) { try { URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Charset", "UTF-8"); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = ""; while ((readLine = br.readLine()) != null) { response += readLine; } br.close(); return response; } catch (Exception e) { e.printStackTrace(); return null; } } }