




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android 网络编程-STRUTS2,JSON,HttpClient 分类: Android智能手机开发 2011-04-21 15:26 243人阅读 评论(0) 收藏 举报 在Android开发过程中,我们需要访问网络上的Web资源,比如网络上的WEB请求。在这里Android就好像是一个终端,可以用来接收Web服务器端发送过来的数据。下面我以Struts2作为Web服务器端的Web框架。来说明Android客户端接收Web请求的过程。首先,我们要配置Web服务器端,添加Struts2所需要的JAR包(包括JSON包)下面是服务器端所要配置的JAR包,如下所示:我们看看json包,有如下: json-lib-*.jdk15.jar,struts2-json-plugin-*.jar,ezmorph-*.jar接下配置web.xml文件,代码如下所示:view plaincopy to clipboardprint?struts2org.apache.struts2.dispatcher.FilterDispatcherstruts2/*添加完JAR包后。我们来配置一下struts.xml文件,这个文件存放在src根目录下面,代码如下所示:view plaincopy to clipboardprint?1. 2. 5. 6. !-settingencoding,DynamicMethod,language7. 8. -9. 10. 12. 13. 17. 18. 19. 20. 22. 23. 24. 25. 26. 看看这个action 返回的是json 数据。而且是由LoginAction类去处理,它存放在com.dongzi.action下面。LoginAction类的代码如下:view plaincopy to clipboardprint?publicclassLoginActionextendsActionSupportimplementsServletRequestAware,ServletResponseAware/*/privatestaticfinallongserialVersionUID=1L;HttpServletRequestrequest;HttpServletResponseresponse;publicvoidsetServletRequest(HttpServletRequestrequest)this.request=request;publicvoidsetServletResponse(HttpServletResponseresponse)this.response=response;publicvoidlogin()try/HttpServletRequestrequest=ServletActionContext.getRequest(); /HttpServletResponseresponse=ServletActionContext.getResponse(); this.response.setContentType(text/html;charset=utf-8);this.response.setCharacterEncoding(UTF-8);/将要返回的实体对象进行json处理 /JSONObjectjson=JSONObject.fromObject(this.getUsername(); /输出格式如:id:1,username:zhangsan,pwd:123 /System.out.println(json); /this.response.getWriter().write(json.toString(); /username:mingg,password:123 JSONObjectjson=newJSONObject();/json.put(username,mingg); /json.put(password,123); /【这里在JSON中包含一个Map】 Mapmap=newHashMap();map.put(username,xiaomingg);map.put(password,1234);map.put(state,1);json.put(userbean,map);response.getWriter().write(json.toString();/userbean:username:100196,password:1234453,State:1 /*值的数组people:username:mingg,password:123,email:172,username:jie,password:111,email:172,username:yong,password:1232,email:1*/JSONArrayjsonArray=newJSONArray(); / /JSONObjectjson=newJSONObject(); /json.put(username,mingg); /json.put(password,123); /json.put(email,172); / /JSONObjectjson1=newJSONObject(); /json1.put(username,jie); /json1.put(password,111); /json1.put(email,172); / /JSONObjectjson2=newJSONObject(); /json2.put(username,yong); /json2.put(password,1232); /json2.put(email,1); / /jsonArray.add(0,json); /jsonArray.add(1,json1); /jsonArray.add(2,json2); / / /JSONObjectalObject=newJSONObject(); /alObject.put(people,jsonArray); /*programmers:*username:mingg,password:123,email:172,*username:jie,password:111,email:172,*username:yong,password:1232,email:1,*authors:*username:mingg,password:123,genre:sciencefiction,*username:jie,password:111,genre:fantasy,*username:yong,password:1232,genre:christianfiction,*musicians:*username:mingg,password:123,instrument:guitar,*username:jie,password:111,instrument:piano,*username:yong,password:1232,instrument:flute*/JSONArrayjsonArray=newJSONArray(); / /JSONObjectjson=newJSONObject(); /json.put(username,mingg); /json.put(password,123); /json.put(email,172); / /JSONObjectjson1=newJSONObject(); /json1.put(username,jie); /json1.put(password,111); /json1.put(email,172); / /JSONObjectjson2=newJSONObject(); /json2.put(username,yong); /json2.put(password,1232); /json2.put(email,1); / /jsonArray.add(0,json); /jsonArray.add(1,json1); /jsonArray.add(2,json2); / / / /JSONArrayjsonArray2=newJSONArray(); / /JSONObjectjson20=newJSONObject(); /json20.put(username,mingg); /json20.put(password,123); /json20.put(genre,sciencefiction); / /JSONObjectjson21=newJSONObject(); /json21.put(username,jie); /json21.put(password,111); /json21.put(genre,fantasy); / /JSONObjectjson22=newJSONObject(); /json22.put(username,yong); /json22.put(password,1232); /json22.put(genre,christianfiction); / /jsonArray2.add(0,json20); /jsonArray2.add(1,json21); /jsonArray2.add(2,json22); / / /JSONArrayjsonArray3=newJSONArray(); / /JSONObjectjson30=newJSONObject(); /json30.put(username,mingg); /json30.put(password,123); /json30.put(instrument,guitar); / /JSONObjectjson31=newJSONObject(); /json31.put(username,jie); /json31.put(password,111); /json31.put(instrument,piano); / /JSONObjectjson32=newJSONObject(); /json32.put(username,yong); /json32.put(password,1232); /json32.put(instrument,flute);/笛 / /jsonArray3.add(0,json30); /jsonArray3.add(1,json31); /jsonArray3.add(2,json32); / / /JSONObjectalObject=newJSONObject(); /alObject.put(programmers,jsonArray); /alObject.put(authors,jsonArray2); /alObject.put(musicians,jsonArray3); /*yong:1232:1*/获取任意节点的值:试例,第个节点,第三个子节点 /JSONArrayjsonArrayt11=(JSONArray)alObject.get(programmers); / /JSONObjectjsonObject11=(JSONObject)jsonArrayt11.get(2); / /Stringusername=jsonObject11.getString(username); /Stringpassword=jsonObject11.getString(password); /Stringemail=jsonObject11.getString(email); / /StringBuffersBuffer=newStringBuffer(); /sBuffer.append(username+:).append(password+:).append(email); /response.getWriter().write(username.toString(); /*JSONObjectjson=newJSONObject();json.put(login,login);*response.setContentType(text/html;charset=utf-8);*System.out.println(json);bytejsonBytes=*json.toString().getBytes(utf-8);*response.setContentLength(jsonBytes.length);*response.getOutputStream().write(jsonBytes);*/JSONObjectjson=newJSONObject(); /json.put(login,login); /bytejsonBytes=json.toString().getBytes(utf-8); /response.setContentType(text/html;charset=utf-8); /response.setContentLength(jsonBytes.length); /response.getOutputStream().write(jsonBytes); /response.getOutputStream().flush(); /response.getOutputStream().close(); catch(Exceptione)e.printStackTrace();/returnnull; 我下面简单说一下JSON解析过程。 通过访问http:/localhost:8888/AndroidServerApp/login.action,得到如下JSON数据:JSONObject json=new JSONObject(); /【这里在JSON中包含一个Map】 Map map=new HashMap(); map.put(username, xiaomingg); map.put(password, 1234); map.put(state, 1); json.put(userbean, map); response.getWriter().write(json.toString();服务器端的配置完成了。下面我来配置android客户端了。由于Android内置提拱了解析JSON数据的包。所以就不需要使用第三方包了Android 访问网络资源的代码如下所示:view plaincopy to clipboardprint?privatestaticStringurl=:8888/AndroidServerApp/login.action;getPDAServerData(url);privatevoidgetPDAServerData(Stringurl)HttpClientclient=newDefaultHttpClient();/提拱默认的HttpClient实现 HttpPostrequest;tryrequest=newHttpPost(newURI(url);HttpResponseresponse=client.execute(request);/判断请求是否成功 if(response.getStatusLine().getStatusCode()=200)/200表示请求成功 HttpEntityentity=response.getEntity();if(entity!=null)Stringout=EntityUtils.toString(entity);JSONObjectjsonObject;Stringusername=;Stringpassword=;StringstateStr=;UserBeanuserBean=newUserBean();try/userbean:username:100196,password:1234453,State:1 /JSONObjectjsonObject=newJSONObject(builder.toString().getJSONObject(userbean); jsonObject=newJSONObject(out).getJSONObject(userbean);userBean.setUsername(jsonObject.getString(username);userBean.setPassword(jsonObject.getString(password);userBean.setState(Integer.parseInt(jsonObject.getString(state);catch(JSONExceptione)/TODOAuto-generatedcatchblock e.printStackTrace();newAlertDialog.Builder(this).setMessage(userBean.getUsername()+:+userBean.getState().create().show();catch(URISyntaxExceptione)e.printStackTrace();newAlertDialog.Builder(this).setMessage(e.getMessage().create().show();catch(ClientProtocolExceptione)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025网络设备购销合同文本
- 2025企业劳动合同协议书样本
- (正式版)DB1501∕T 0037-2023 《园林植物翠雀花栽培技术规程》
- 第9课 清末新政说课稿-2025-2026学年高中历史华东师大版上海第五分册-华东师大版上海2007
- 2025委托代理合同范本
- 2025自动化设备安装调试合同书
- 综评高职面试题库及答案
- DB65T 3623-2014 一种采用二维码防伪技术进行动物检疫合格证明(产品A、产品B)电子出证的技术规范
- 昆明烟厂招聘试题及答案
- 苏科版九年级物理上册第十四章四、欧姆定律的应用 说课稿
- 乡镇综合行政执法队队长试用期满转正工作总结
- 2025天津医科大学眼科医院第三批招聘1人备考考试试题及答案解析
- 2025年法院书记员招聘考试笔试试题含答案
- CPK、PPK和SPC(X-R控制图)模板
- 汉诺塔课件教学课件
- 2025年二级建造师考试施工管理真题及答案
- 穿线施工方案(3篇)
- 光伏发电运行维护定期巡视检查项目和周期
- 特种设备(锅炉、压力容器)培训考试试题及答案
- 2025年北京市高考英语试卷真题(含答案解析)
- 医务科依法执业自查表
评论
0/150
提交评论