如何用HttpClient post方法传递一个POJO对象?
POJO类如下:publicclassUserDTOimplementsjava.io.Serializable{privatestaticfinallongserial...
POJO类如下:
public class UserDTO implements java.io.Serializable {
private static final long serialVersionUID = 1L;
/**
* 序号
*/
private Integer seqNo;
/**
* 用户ID
*/
private Integer tempuserid;
/**
* 用户名称
*/
private String userName;
/**
* 客户端类型
*/
private Integer clientType;
/**
* Pin code
*/
private Integer pinCode;
/**
* 域
*/
private Integer domain;
/**
* Ip地址
*/
private String ipaddr;
……省略getter/setter方法
}
HttpClient方法部分如下:
PostMethod postMethod = new PostMethod(URL);
postMethod.addRequestHeader("content-type", "application/xml");
//TODO 需要将POJO实体对象添加到请求体中
httpClient.executeMethod(postMethod); 展开
public class UserDTO implements java.io.Serializable {
private static final long serialVersionUID = 1L;
/**
* 序号
*/
private Integer seqNo;
/**
* 用户ID
*/
private Integer tempuserid;
/**
* 用户名称
*/
private String userName;
/**
* 客户端类型
*/
private Integer clientType;
/**
* Pin code
*/
private Integer pinCode;
/**
* 域
*/
private Integer domain;
/**
* Ip地址
*/
private String ipaddr;
……省略getter/setter方法
}
HttpClient方法部分如下:
PostMethod postMethod = new PostMethod(URL);
postMethod.addRequestHeader("content-type", "application/xml");
//TODO 需要将POJO实体对象添加到请求体中
httpClient.executeMethod(postMethod); 展开
展开全部
http请求传递的都是字符串。这种情况一般使用xml为信息载体。把java类转成xml, client得到xml再转成java类。
UserDTO user = new UserDTO();
...//填充user
XStream xstream = new XStream();//使用xstream转换pojo和xml字符串
// HttpPost连接对象
String httpUrl = “http://127.0.0.1:8080/hello/hello.jsp”;
HttpPost httpRequest=new HttpPost(httpUrl);
//使用NameValuePair来保存要传递的Post参数
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user",xstream.toXML(user)));
//设置字符集
HttpEntity httpentity=new UrlEncodedFormEntity(params,"gb2312");
//取得HttpClient对象
HttpClient httpclient=new DefaultHttpClient();
//请求HttpCLienthttpResponse=httpclient.execute(httpRequest); //请求成功 if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK) { String strResult=EntityUtils.toString(httpResponse.getEntity());
UserDTO user2 = xstream.fromXML(strResult);
}
---------使用xstream可以很方便的转换java对象和xml字符串
UserDTO user = new UserDTO();
...//填充user
XStream xstream = new XStream();//使用xstream转换pojo和xml字符串
// HttpPost连接对象
String httpUrl = “http://127.0.0.1:8080/hello/hello.jsp”;
HttpPost httpRequest=new HttpPost(httpUrl);
//使用NameValuePair来保存要传递的Post参数
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user",xstream.toXML(user)));
//设置字符集
HttpEntity httpentity=new UrlEncodedFormEntity(params,"gb2312");
//取得HttpClient对象
HttpClient httpclient=new DefaultHttpClient();
//请求HttpCLienthttpResponse=httpclient.execute(httpRequest); //请求成功 if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK) { String strResult=EntityUtils.toString(httpResponse.getEntity());
UserDTO user2 = xstream.fromXML(strResult);
}
---------使用xstream可以很方便的转换java对象和xml字符串
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询