AndroidNote010.Android访问webservice(上)服务器端
来源:互联网 发布:印刷厂计件软件 编辑:程序博客网 时间:2024/06/05 16:33
思路:使用XFire搭建了一个webservice服务器,android客户端访问服务器,服务器返回的json数据对象。
今天主要实现了一个保存和查询的功能,在介绍之前,先要做点准备工作。
因为在android中没有提供直接与webservice相互调用的类库,必须依靠第三方类库才可以完成,这里使用
的是ksoap类库。关于json数据的解析,这里使用谷歌的gson,非常好用,在之前的博客里已经介绍过了,
这里就不多说了。
特别要注意的是编码问题,一定要统一编码,不然很容易出现乱码,这里使用utf8.
另外,在访问数据的时候路径这样写
public static final String DBURL = "jdbc:mysql://localhost:3307/androiddb?useUnicode=true&characterEncoding=utf8";在服务器端,要创建实体类
package seu.xin.entity;import java.io.Serializable;public class UserInfo implements Serializable {private static final long serialVersionUID = 1L;private int uid;private String name;private String password;private String mail;private String weibo;private String city;private String distance;public int getUid() {return uid;}@Overridepublic String toString() {return "UserInfo [uid=" + uid + ", name=" + name + ", password="+ password + ", mail=" + mail + ", weibo=" + weibo + ", city="+ city + ", distance=" + distance + "]";}public void setUid(int uid) {this.uid = uid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getMail() {return mail;}public void setMail(String mail) {this.mail = mail;}public String getWeibo() {return weibo;}public void setWeibo(String weibo) {this.weibo = weibo;}public String getDistance() {return distance;}public void setDistance(String distance) {this.distance = distance;}public void setCity(String city) {this.city = city;}public String getCity() {return city;}}还要创建与数据库操作相关的类,主要是增删改查的操作
package seu.xin.db;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import seu.xin.entity.UserInfo;public class UserInfoDB {public static final String DBDRIVER = "com.mysql.jdbc.Driver";public static final String DBURL = "jdbc:mysql://localhost:3307/androiddb?useUnicode=true&characterEncoding=utf8";public static final String DBUSER = "root"; public static final String DBPASS = "123456"; // get connection private Connection getConnection() { try {Class.forName(DBDRIVER);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {return DriverManager.getConnection(DBURL, DBUSER, DBPASS);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null; } //save a userinfo data public void save (UserInfo userInfo){ Connection con = this.getConnection(); String sql = "insert into userinfo(uid,name,password,mail,weibo,city,distance) values(?,?,?,?,?,?,?)"; try {PreparedStatement ps = con.prepareStatement(sql);ps.setInt(1, userInfo.getUid());ps.setString(2, userInfo.getName());ps.setString(3, userInfo.getPassword());ps.setString(4, userInfo.getMail());ps.setString(5, userInfo.getWeibo());ps.setString(6, userInfo.getCity());ps.setString(7, userInfo.getDistance());ps.executeUpdate();System.out.println("save UserInfo success!!");con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("save exception");} } //update a userinfo date public void update(UserInfo userInfo){ Connection con = this.getConnection(); String sql = "update userinfo set name=?,password=?,mail=?,weibo=?,city=?,distance=? where uid=?"; try {PreparedStatement ps = con.prepareStatement(sql);ps.setString(1, userInfo.getName());ps.setString(2, userInfo.getPassword());ps.setString(3, userInfo.getMail());ps.setString(4, userInfo.getWeibo());ps.setString(5, userInfo.getCity());ps.setString(6, userInfo.getDistance());ps.setInt(7, userInfo.getUid());ps.executeUpdate();System.out.println("update userinfo success!!");con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("update exception");} }// delete a userinfo data public void delete(UserInfo userInfo){ Connection con = this.getConnection(); String sql = "delete from userinfo where uid=?"; try {PreparedStatement ps = con.prepareStatement(sql);ps.setInt(1, userInfo.getUid());ps.execute();System.out.println("delete userinfo successS");con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("delete exception");} } //find a user by id public UserInfo findByName(String name){ UserInfo userInfo = null; Connection con = this.getConnection(); String sql = "select * from userinfo where name = ?"; try {PreparedStatement ps = con.prepareStatement(sql);ps.setString(1, name);ResultSet rs = ps.executeQuery();if(rs.next()){userInfo = new UserInfo();userInfo.setUid(rs.getInt("uid"));userInfo.setName(rs.getString("name"));userInfo.setPassword(rs.getString("password"));userInfo.setMail(rs.getString("mail"));userInfo.setWeibo(rs.getString("weibo"));userInfo.setCity(rs.getString("city"));userInfo.setDistance(rs.getString("distance"));}con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return userInfo; } public ArrayList<UserInfo> getAllUsers(){ ArrayList<UserInfo> arrayList = new ArrayList<UserInfo>(); UserInfo userInfo = null; Connection con = this.getConnection(); String sql = "select * from userinfo"; try {PreparedStatement ps = con.prepareStatement(sql);ResultSet rs = ps.executeQuery();while(rs.next()){userInfo = new UserInfo();userInfo.setUid(rs.getInt("uid"));userInfo.setName(rs.getString("name"));userInfo.setPassword(rs.getString("password"));userInfo.setMail(rs.getString("mail"));userInfo.setWeibo(rs.getString("weibo"));userInfo.setCity(rs.getString("city"));userInfo.setDistance(rs.getString("distance"));arrayList.add(userInfo);}con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("getAllUsers exception");}return arrayList; } }
然后就是创建service类,这里主要是两个方法save和findUserByName
package seu.xin.service;import com.google.gson.Gson;import seu.xin.db.UserInfoDB;import seu.xin.entity.UserInfo;public class UserInfoService {public void save(String name,String password){UserInfoDB userInfoDB = new UserInfoDB();UserInfo userInfo = new UserInfo();userInfo.setName(name);userInfo.setPassword(password);userInfoDB.save(userInfo);System.out.println("UserInfoService save success!!");}public String findUserByName(String name){UserInfoDB userInfoDB = new UserInfoDB();UserInfo userInfo = new UserInfo();userInfo = userInfoDB.findByName(name);Gson gson = new Gson();String json = gson.toJson(userInfo);System.out.println("UserInfoService findUserByName json!!"+json);return json;} }
在findUserByName方法中,返回的就是json对象,这里把之前的UserInfo类转化为json对象
Gson gson = new Gson();String json = gson.toJson(userInfo);之后,配置XML文件,发布到服务器上就可以使用了。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://xfire.codehaus.org/config/1.0"><service><name>webservice</name><serviceClass>seu.xin.service.UserInfoService</serviceClass><style>wrapped</style><use>literal</use><scope>application</scope></service></beans>
- AndroidNote010.Android访问webservice(上)服务器端
- android客户端访问服务器端(SSH框架)
- android 客户端访问服务器端
- android 客户端访问服务器端
- android客户端程序访问服务器端webservice,几篇不错的文章!
- android通过webservice连接SQL数据库(一)服务器端
- 学会使用Web Service上(服务器端访问)
- 转载:学会使用Web Service上(服务器端访问)
- android访问服务器端上传及服务器端接收
- android访问服务器端上传及服务器端接收
- WebService之HelloWorld(服务器端)
- Android访问WebService
- Android访问webservice
- android 访问WebService(转载)
- Android访问WebService
- Android访问webService浅例
- Android访问调用Webservice
- Android访问WebService
- fedora9下使用QT连接数据库(QSqlDatabase: QODBC driver not loaded)
- 内存。二重指针,指针的高级使用
- 计算机图形学系列相关文章索引(持续更新)
- VC++ Progress Control
- 自己写的一个启动JBoss服务器的bat批处理
- AndroidNote010.Android访问webservice(上)服务器端
- POI框架导出EXCEL的简单列子(跨行跨列)合并单元格
- swing jtable常用
- windows 2003 SOCKET 端口数量默认5000
- 视频跟踪算法简介
- NAND Flash的坏块管理设计
- Java中的volatile关键字
- STL map, STL set之数据结构基础
- 动态规划总结