客户端:
服务器端接收:
操纵数据库:
配置:
@Override
protected String doInBackground(String... params) {
String result = null;
JSONObject obj = new JSONObject();
HzzyHttpClient client = new HzzyHttpClient();
List<NameValuePair> paramss = new ArrayList<NameValuePair>();
paramss.add(new BasicNameValuePair("MLPMC", addressData.getMLPMC()));
paramss.add(new BasicNameValuePair("JLXDM", addressData.getJLXDM()));
paramss.add(new BasicNameValuePair("BZLX", addressData.getBZLX()));
paramss.add(new BasicNameValuePair("ZBX", addressData.getZBX()));
paramss.add(new BasicNameValuePair("ZBY", addressData.getZBY()));
try {
//到时候替换到该地址
//result = client.Postcs(PQGLMapActivity.serverurl+"/PQGLService/rest/addressCollection/collecteBuilding", paramss);
result = client.Postcs("Http://192.168.1.166:8080/PQGLService/rest/addressCollection/collecteBuilding", paramss);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
客户端请求:
import java.net.URLEncoder;
import java.util.List;
import org.apache.http.HttpResponse;
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.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class HzzyHttpClient {
public String Post(String url) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
String response = null;
try {
// 设置httppost请求参数
// httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
// 使用execute方法发送httppost请求,返回HttpResponse对象
HttpResponse httpResponse = httpclient.execute(httppost);
if (httpResponse != null) {
response = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public String Postcs(String url, List<NameValuePair> params)
throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
String response = null;
try {
// 设置httppost请求参数
httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 使用execute方法发送httppost请求,返回HttpResponse对象
HttpResponse httpResponse = httpclient.execute(httppost);
if (httpResponse != null) {
response = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public String get(String url, List<NameValuePair> params) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
url += "?";
String urlParam = "";
for (int i = 0; i < params.size(); i++) {
NameValuePair nvPair = params.get(i);
urlParam += nvPair.getName() + "="
+ URLEncoder.encode(nvPair.getValue()) + "&";
}
url += urlParam;
HttpGet httpget = new HttpGet(url);
String response = null;
try {
HttpResponse httpResponse = httpclient.execute(httpget);
if (httpResponse != null) {
response = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public String get(String url) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
String response = null;
try {
HttpResponse httpResponse = httpclient.execute(httpget);
if (httpResponse != null) {
response = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public String get(String url, int flag) throws Exception {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpGet httpget = new HttpGet(url);
String response = null;
try {
HttpResponse httpResponse = httpclient.execute(httpget);
if (httpResponse != null) {
response = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
}
服务器端接收:
@Path("addressCollection")
public class AddressCollectionResource {
private IAddressCollectionService addressCollectionService;
public void setService(IAddressCollectionService addressCollectionService) {
this.addressCollectionService = addressCollectionService;
}
public IAddressCollectionService getAddressCollectionService() {
return addressCollectionService;
}
public void setAddressCollectionService(
IAddressCollectionService addressCollectionService) {
this.addressCollectionService = addressCollectionService;
}
/**
* 采集幢/门楼牌 url:Http://localhost:8080/PQGLService/rest/addressCollection/
* collecteBuilding
*
* @param mlpmc
* @param jlxdm
* @param bzlx
* @param zbx
* @param zby
* @param servletResponse
* @return
*/
@Path("collecteBuilding")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String AddBuilding(@FormParam("MLPMC") String mlpmc,
@FormParam("JLXDM") String jlxdm, @FormParam("BZLX") String bzlx,
@FormParam("ZBX") String zbx, @FormParam("ZBY") String zby,
@Context HttpServletResponse servletResponse) {
String result = "fail";
try {
T_DZ_MLPH mlph = new T_DZ_MLPH();
mlph.setMLPMC(mlpmc);
mlph.setJLXDM(jlxdm);
mlph.setBZLX(bzlx);
mlph.setZBX(zbx);
mlph.setZBY(zby);
result = addressCollectionService.saveAddress(mlph);
} catch (Exception ex) {
}
return result;
}
}
操纵数据库:
public class AddressCollectionService implements IAddressCollectionService {
private IHibernateDao hibernateDao;
private IJdbcDao jdbcDao;
public void setHibernateDao(IHibernateDao hibernateDao) {
this.hibernateDao = hibernateDao;
}
public IJdbcDao getJdbcDao() {
return jdbcDao;
}
public void setJdbcDao(IJdbcDao jdbcDao) {
this.jdbcDao = jdbcDao;
}
public IHibernateDao getHibernateDao() {
return hibernateDao;
}
@Override
public String saveAddress(T_DZ_MLPH mlph) {
//insert into t_dz_mlph (mlpdm) values (sid_t_dz_mlph_mlpdm.nextval)
String sql = "insert into T_DZ_MLPH(MLPDM,JLXDM,BZLX,ZBX,ZBY,MLPMC) values (sid_t_dz_mlph_mlpdm.nextval,'"+mlph.getMLPMC()+"','"
+ mlph.getJLXDM() + "','" + mlph.getBZLX() + "','"
+ mlph.getZBX() + "','" + mlph.getZBY() + "')";
try{
jdbcDao.execute(sql);
}catch(Exception e){
e.printStackTrace();
}
return "success";
}
}
配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- <bean id="fileUpandLoadService" class="com.hzzy.pqgl.service.impl.FileUpandDownService"> -->
<!-- <property name="hibernateDao" ref="hibernateDao" /> -->
<!-- </bean> -->
<bean id="fileUpandLoadService" parent="txProxyTemplate">
<property name="target">
<bean class="com.hzzy.pqgl.service.impl.FileUpandDownService">
<property name="hibernateDao">
<ref bean="hibernateDao" />
</property>
<property name="jdbcDao">
<ref bean="jdbcDao" />
</property>
</bean>
</property>
</bean>
<bean id="userResource" class="com.hzzy.pqgl.resources.UserResourceService">
<property name="hibernateDao" ref="hibernateDao" />
</bean>
<bean id="fileuploadResource" class="com.hzzy.pqgl.resources.FileUploadResource">
<property name="fileUpandLoadService" ref="fileUpandLoadService" />
</bean>
<bean id="filedownloadResource" class="com.hzzy.pqgl.resources.FileDownloadRecource">
<property name="fileUpandLoadService" ref="fileUpandLoadService" />
</bean>
<bean id="queryUserService" parent="txProxyTemplate">
<property name="target">
<bean class="com.hzzy.pqgl.service.impl.QueryUserService">
<property name="hibernateDao">
<ref bean="hibernateDao" />
</property>
<property name="jdbcDao">
<ref bean="jdbcDao" />
</property>
</bean>
</property>
</bean>
<bean id="addressCollectionService" parent="txProxyTemplate">
<property name="target">
<bean class="com.hzzy.pqgl.service.impl.AddressCollectionService">
<property name="hibernateDao">
<ref bean="hibernateDao" />
</property>
<property name="jdbcDao">
<ref bean="jdbcDao" />
</property>
</bean>
</property>
</bean>
<!-- <bean id="queryUserResource" class="com.hzzy.pqgl.resources.QueryUserResource">
<property name="queryUserService" ref="queryUserService"></property> </bean> -->
<bean id="addressCollectionResource" class="com.hzzy.pqgl.resources.AddressCollectionResource">
<property name="addressCollectionService" ref="addressCollectionService"></property>
</bean>
</beans>