该篇文章主要说明使用Apache提供的HTTPClient,通过post方式,向服务器发送数据.由于有些东西在 中提到过,就不再重复.
一,Android客户端的业务逻辑层:
package spt.http.post;import java.io.IOException;import java.net.MalformedURLException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.http.HttpResponse;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import android.os.Handler;import android.util.Log;/** * 用户向服务器端发送数据的类(使用post)方法. * * @author Administrator * */public class PostSender { // 连接服务器的url. private static final String URL = "http://192.168.1.101:8080/ReceiveAndroid/ServletForPostMethod"; // 标识是否连接到服务器成功. public static final int SEND_SUCCESS = 1; public static final int SEND_FAIL = 0; private Handler handler = null; //新线程关联的Handler,用于将是否发送成功的标识Message放到消息队列中. public PostSender(Handler handler) { this.handler = handler; } /** * 往服务器发送数据. * * @param name * @param pwd */ public void send(String name, String pwd) { // 这里params要传递到另外一个方法,加final为了防止被修改. final Mapparams = new HashMap (); params.put("name", name); params.put("pwd", pwd); // 启动新的线程连接服务器. new Thread(new Runnable() { @Override public void run() { // 请求连接. try { if (postSend(params, URL, "utf-8")) handler.sendEmptyMessage(SEND_SUCCESS); else handler.sendEmptyMessage(SEND_FAIL); } catch (MalformedURLException e) { Log.d("sysout", "run:MalformedURLException" + e.getMessage()); } catch (IOException e) { Log.d("sysout", "run:IOException" + e.getMessage()); } } }).start(); } /** * 发送post请求的方法. * * @param params * 请求参数的键-值对. * @param url * @param encoding * 使用指定编码对参数值进行编码. * @return * @throws MalformedURLException * @throws IOException */ private boolean postSend(Map params, String url, String encoding) throws MalformedURLException, IOException { // 封装请求参数的键值对. List pairs = new ArrayList (); for (Entry param : params.entrySet()) { pairs.add(new BasicNameValuePair(param.getKey(), param.getValue())); } //封装请求参数的实体. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encoding); //使用post请求. HttpPost post = new HttpPost(url); post.setEntity(entity); //使用DefaultHttpClient指定请求,以获取响应信息. DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(post); return response.getStatusLine().getStatusCode() == 200; // 等于200表示发送成功. }}
二,服务器端的Servlet,重写doPost方法:
package spt.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/ServletForPostMethod")public class ServletForPostMethod extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("enter...post"); String name = req.getParameter("name"); String pwd = req.getParameter("pwd"); System.out.println("name=" + name); System.out.println("pwd=" + pwd); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// System.out.println("enter...get");// String name = req.getParameter("name");// String pwd = req.getParameter("pwd");// System.out.println("name=" + name);// System.out.println("pwd=" + pwd); }}
另外,途中,我连几个小时的Tomcat服务器,后来通过log过滤,发现自己的问题,所以建议多使用log捕获异常信息.我在网上看到有位网友,使用HTTP进行post连接的时候,对url的处理和get一样,都是将参数拼接在原有url的后面,我觉得很奇怪.