已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 婚姻家庭咨询师初级面试准备资料
- 楦头出格师晋升之路从初级到高级
- 健康照护师初级心理疏导服务计划
- Q2财务分析项目成果汇报与总结报告
- 高级乡村直播销售员销售目标制定与达成计划
- 建筑信息模型BIM技术应用
- 员工绩效考核管理制度及绩效改进计划
- 叉车安全培训讲义中文
- 新员工陶瓷烧成工岗位入职培训计划
- 企业绩效考核体系优化方案及实施指南
- 猫武士三部曲 4天蚀遮月
- 《内河航道引航》考试复习(重点)题库(300题)
- 工程整改通知单问题整改通知单
- 降低手术患者术中低体温发生率
- 大班语言-蜂蜜失窃谜案
- 中国地质大学地球科学概论教学课程pptpart5公开课获奖课件
- YY/T 0801.1-2010医用气体管道系统终端第1部分:用于压缩医用气体和真空的终端
- 初中数学思维能力的培养
- (完整)电厂安全教育知识题库及答案(通用版)
- 地质灾害培训班滑坡防治工程勘查规范
- 高支模验收表
评论
0/150
提交评论