
Android从服务器端获取数据的几种方法.doc
7页Android从效劳器端获取数据的几种方法 在android中有时候我们不需要用到本机的SQLite数据库提供数据,更多的时候是从网络上获取数据,那么Android怎么从效劳器端获取数据呢?有很多种,归纳起来有 一:基于 协议获取数据方法二:基于SAOP协议获取数据方法,三:忘了------- 那么我们的这篇文章主要是将关于使用 协议获取效劳器端数据,这里我们采取的效劳器端技术为java,框架为Struts2,或者可以有Servlet,又或者可直接从JSP页面中获取数据 那么,接下来我们便开始这一路程: 首先:编写效劳器端方法,我这里采用的MVC框架是Struts2,目的很单纯,就是为了以后做个完整的商业工程,技术配备为:android+SSH当然,篇幅有限,我这里就直接用Strtus2而已 效劳器端:新建WebProject ,选择Java ee 5.0. 为了给工程添加Struts2的支持,我们必须导入Struts2的一些类库,如下即可〔有些jar包是不必的,但是我们后来扩展可能是要使用到的,就先弄进去〕: 1: xwork-core-.1.jar 2: struts2-core- 3: commons-logging-.jar 4: freemarker-.jar 7:commons-ileupload.jar 9:json-lib-2.1-jdk15.jar 处理JSON格式数据要使用到 10:struts2-json-plugin-.1.jar 基于struts2的json插件 以上的jar包,需要放在WebRoot/WEB-INF/lib目录下 然后在web.xml文件中敲下: View Code
方法为method对应的login,类名为loginAction, 注意:包继承为:json-default ,输出结果类型为json 如下: View Code public class loginAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {/*** */private static final long serialVersionUID = 1L; ServletRequest request; ServletResponse response;public void setServletRequest( ServletRequest request) {this.request=request;}public void setServletResponse( ServletResponse response) {this.response=response;}public void login(){ try {// ServletRequest request =ServletActionContext.getRequest();// ServletResponse response=ServletActionContext.getResponse();this.response.setContentType("text/html;charset=utf-8");this.response.setCharacterEncoding("UTF-8");if(this.request.getParameter("username").equals("123456")){this.response.getWriter().write("真的很奇怪,日本人!");}else if(this.request.getParameter("username").equals("zhd")){this.response.getWriter().write("没有错,我就是东子哥!");}else{this.response.getWriter().write("我就是东子哥!");}//将要返回的实体对象进行json处理 // JSONObject json=JSONObject.fromObject(this.getUsername()); //输出格式如:{"id":1, "username":"zhangsan", "pwd":"123"} // System.out.println(json); // this.response.getWriter().write(json.toString());/**JSONObject json=new JSONObject(); json.put("login", "login");response.setContentType("text/html;charset=utf-8");System.out.println(json);byte[] jsonBytes = json.toString().getBytes("utf-8");response.setContentLength(jsonBytes.length);response.getOutputStream().write(jsonBytes);**//**JSONObject json=new JSONObject(); json.put("login", "login");byte[] jsonBytes = 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 (Exception e) { e.printStackTrace();}// return null;}} 运行查看下: ://localhost:8080/PDAServer/login.action?username=123456 当然你可以输入其他参数的URL 运行成功。
客户端: 这里需要注意的是模拟器把自己当成了localhost,以及127.0.0.1了,因此如果基于本地的web工程测试的话,必须修改IP为:10.0.2.2 public class MainActivity extends Activity {/** Called when the activity is first created. *///模拟器自己把自己当成localhost了,效劳器应该为10.0.2.2private static String url=" ://10.0.2.2:8080/PDAServer/login.action";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceSt。





![河南新冠肺炎文件-豫建科[2020]63号+豫建科〔2019〕282号](http://img.jinchutou.com/static_www/Images/s.gif)






