HttpUtil:
package org.crazyit.auction.client.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/**
* Description: <br/>
* 网站: <a href="http://www.crazyit.org">疯狂ava联盟</a> <br/>
* Copyright (C), 2001-2012, Yeeku.H.Lee <br/>
* This program is protected by copyright laws. <br/>
* Program Name: <br/>
* Date:
*
* @author Yeeku.H.Lee [email protected]
* @version 1.0
*/
public class HttpUtil {
/**
* 创建HttpClient对象
*/
public static HttpClient httpClient = new DefaultHttpClient();
/**
* 用户登录的URL请求地址
*/
public static final String LOGIN_URL = "http://192.168.1.202:8080/GourmetOrderServer/loginServlet";
/**
* 用户注册的URL请求地址
*/
public static final String REGISTER_URL = "http://192.168.1.202:8080/GourmetOrderServer/registerServlet";
/**
* 下订单的URL请求地址
*/
public static final String XIADAN_URL = "http://192.168.1.202:8080/GourmetOrderServer/orderServlet";
/**
* 信息更新的URL请求地址
*/
public static final String UPDATE_URL = "http://192.168.1.202:8080/GourmetOrderServer/updateServlet";
/**
* GET方法发送请求到服务器
*
* @param url
* 发送请求的URL
* @return 服务器响应字符串
* @throws Exception
*/
public static String getRequest(String url) throws Exception {
String result = "";
// 创建HttpGet对象。
HttpGet request = new HttpGet(url);
// 发送GET请求
HttpResponse httpResponse = httpClient.execute(request);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 获取服务器响应字符串
result = EntityUtils.toString(httpResponse.getEntity());
}
return result;
}
/**
*
* @param url
* 发送请求的URL
* @param params
* 请求参数
* @return 服务器响应字符串
* @throws Exception
*/
public static String postRequests(String url, Map<String, String> rawParams)
throws Exception {
String result = "";
// 创建HttpPost对象。
HttpPost post = new HttpPost(url);
// 如果传递参数个数比较多的话可以对传递的参数进行封装
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (String key : rawParams.keySet()) {
// 封装请求参数
params.add(new BasicNameValuePair(key, rawParams.get(key)));
}
// 设置请求参数
post.setEntity(new UrlEncodedFormEntity(params, "gbk"));
// 发送POST请求
HttpResponse httpResponse = httpClient.execute(post);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 获取服务器响应字符串
result = EntityUtils.toString(httpResponse.getEntity());
}
return result;
}
/**
* POST方法发送请求到服务器
*
* @param url
* 发送请求的URL
* @param params
* 键值对集合
* @return 从服务器返回的字符串
*/
public static String postRequest(String url, List<NameValuePair> params) {
String result = "";
try {
// 创建HttpPost对象。
HttpPost request = new HttpPost(url);
// 设置请求参数
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 发送POST请求
HttpResponse response = httpClient.execute(request);
// 如果服务器成功地返回响应
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 获取服务器响应字符串
result = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
User:
package com.gourmetMeal.entry;
/**
* 用户信息的实体
*
* @author bcu
*
*
*/
public class User {
/**
* 用户名
*/
protected String user_name;
/**
* 用户密码
*/
private String user_pwd;
/**
* 用户性别
*/
private String user_sex;
/**
* 用户出生日期
*/
private String user_birthday;
/**
* 用户手机号
*/
private String user_phone;
/**
* 无参构造器
*/
public User() {
}
/**
* 全参构造器
*
* @param user_name
* 用户名
* @param user_pwd
* 用户密码
* @param user_sex
* 用户性别
* @param user_birthday用户出生日期
* @param user_phone
* 用户电话
*/
public User(String user_name, String user_pwd, String user_sex,
String user_birthday, String user_phone) {
super();
this.user_name = user_name;
this.user_pwd = user_pwd;
this.user_sex = user_sex;
this.user_birthday = user_birthday;
this.user_phone = user_phone;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_pwd() {
return user_pwd;
}
public void setUser_pwd(String user_pwd) {
this.user_pwd = user_pwd;
}
public String getUser_sex() {
return user_sex;
}
public void setUser_sex(String user_sex) {
this.user_sex = user_sex;
}
public String getUser_birthday() {
return user_birthday;
}
public void setUser_birthday(String user_birthday) {
this.user_birthday = user_birthday;
}
public String getUser_phone() {
return user_phone;
}
public void setUser_phone(String user_phone) {
this.user_phone = user_phone;
}
}