Android 客户端、服务端结合代码

本文介绍了一个Android客户端通过HTTP Post方式向服务器发送数据,并由服务器处理后存入数据库的具体实现过程。客户端使用了HzzyHttpClient类进行HTTP请求,而服务器端则通过@Path和@POST注解来接收客户端发送的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端:
		@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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值