在web端和android端通过JSON格式实现数据交互
来源:互联网 发布:gt designer3数据版本 编辑:程序博客网 时间:2024/06/06 02:15
我的web端服务器使用tomcat,数据库是用的Oracle,利用struts2实现页面跳转。采用MVC模式,有vo层,dao层,和Service层,在Action中调用Service中的方法,进行增删该查。在数据传输时,android端需要导入Apache提供的jar包:org.json.jar
我只写出Action类的代码:
public class NewsAction extends ActionSupport {private News news;private int pageNo;private int pageSize;public int getPageNo() {return pageNo;}public void setPageNo(int pageNo) {this.pageNo = pageNo;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public News getNews() {return news;}public void setNews(News news) {this.news = news;}public String insertPre() throws Exception {List<NewsType> all = ServiceFactory.getINewsServiceInstance().insertPre();JSONArray array = new JSONArray();Iterator<NewsType> iter = all.iterator();while (iter.hasNext()) {NewsType t = iter.next();JSONObject obj = new JSONObject();obj.put("tid", t.getTid());obj.put("tname", t.getTname());array.put(obj);}HttpServletResponse response = ServletActionContext.getResponse();response.setCharacterEncoding("UTF-8");response.setContentType("text/html");PrintWriter out = response.getWriter();out.print(array);out.close();return null;}public String list() throws Exception {Map<String, Object> map = ServiceFactory.getINewsServiceInstance().list(pageNo, pageSize);List<News> allNews = (List<News>) map.get("allNews");int count = (Integer) map.get("count");JSONObject root = new JSONObject();JSONArray array = new JSONArray();SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");Iterator<News> iter = allNews.iterator();while (iter.hasNext()) {News n = iter.next();JSONObject obj = new JSONObject();obj.put("id", n.getId());obj.put("title", n.getTitle());obj.put("content", n.getContent());obj.put("pubDate", sf.format(n.getPubDate()));obj.put("typeId", n.getTypeId());array.put(obj);}<pre name="code" class="java">在android端写了一个Util工具类,专门用来与web端进行数据交互
public class NetworkUtils {private static final String URL_BASE = "http://192.168.2.106:8080/AndroidNewsDemo/";public static final String GET_ALL_NEWS_TYPE_URL = URL_BASE+ "news!insertPre.action";public static final String NEWS_INSERT_URL = URL_BASE+ "news!insert.action";public static final String NEWS_LIST_URL = URL_BASE + "news!list.action";public static String getDataByUrl(String urlStr) throws Exception {URL url = new URL(urlStr);URLConnection conn = url.openConnection();InputStream is = conn.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(is));String line = null;StringBuilder builder = new StringBuilder();while ((line = reader.readLine()) != null) {builder.append(line);}reader.close();return builder.toString();}public static JSONArray getJSONArrayByUrl(String urlStr) throws Exception {return new JSONArray(getDataByUrl(urlStr));}public static JSONObject getJSONObjectByUrl(String urlStr) throws Exception {return new JSONObject(getDataByUrl(urlStr));}public static String postDataByUrl(String url, Map<String, String> params)throws Exception {// 使用HttpClient来完成post提交HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost(url);// 准备参数集合List<NameValuePair> allParams = new ArrayList<NameValuePair>();Iterator<Entry<String, String>> iter = params.entrySet().iterator();while (iter.hasNext()) {Entry<String, String> entry = iter.next();allParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}// 设置参数post.setEntity(new UrlEncodedFormEntity(allParams));HttpResponse response = client.execute(post);return EntityUtils.toString(response.getEntity());}public static JSONArray postJSONArrayByUrl(String urlStr,Map<String, String> params) throws Exception {return new JSONArray(postDataByUrl(urlStr, params));}public static JSONObject postJSONObjectByUrl(String urlStr,Map<String, String> params) throws Exception {return new JSONObject(postDataByUrl(urlStr, params));}}在Activity类中调用这个NetworkUtils类中的方法就可以实现数据传输。
0 0
- 在web端和android端通过JSON格式实现数据交互
- Android学习笔记:在Android平台上通过JSON格式与服务器端进行数据交互
- Android学习笔记:在Android平台上通过JSON格式与服务器端进行数据交互
- 移动端常用的数据交互格式有xml和json两种方式
- Android端通过usb和pc端互传json数据
- struts2使用ajax实现页面和action之间通过json数据交互
- servlet服务器端和android端json交互
- Android开发:客户端与服务器通过传递和接收json数据进行交互
- Spring MVC通过JSON实现前后台数据交互
- android实现json数据的解析和把数据转换成json格式的字符串
- android实现json数据的解析和把数据转换成json格式的字符串
- android实现json数据的解析和把数据转换成json格式的字符串
- android实现json数据的解析和把数据转换成json格式的字符串
- android实现json数据的解析和把数据转换成json格式的字符串
- Android 与 web后台数据交互之解析XML和JSON
- struts2和前端通过json交互时数据解析问题
- 在Android中操作JSON数据-读取JSON格式数据/创建JSON格式数据
- 使用Json数据进行服务器与android端交互
- myeclipse中部署jsp项目
- C++学习笔记29,引用变量(1)
- dll和so文件区别与构成
- js实现键盘操作对div的移动或改变-------Day43
- FZU Problem 2168 防守阵地 I
- 在web端和android端通过JSON格式实现数据交互
- 【转】TCP的SACK选项功能的详细解读
- linux 动态装载动态库 ldd,ldconfig,ld.so.conf, ld.so.cache
- wangzhan
- FLex中RegExp正则表达式
- JAVA 序列号 反序列化
- ZOJ-1457
- VS2010 LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 的解决方法
- OCP-1Z0-051 补充题库 第18题 DELETE语句的特性