问题描述
小弟初学安卓,遇到了一些问题,求大神帮助关于从服务器取值:在MyEclipse中:定义了一个JSON变量,K是status,值:登陆成功为0,失败为1,但是在Eclipse中接受不到statusMyEclipse:publicvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");intstatus=0;Connectionct=null;Statementsm=null;ResultSetrs=null;Stringname=req.getParameter("name");Stringpassword=req.getParameter("password");try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");ct=DriverManager.getConnection("jdbc:sqlserver://10.10.163.46:1433;DatabaseName=logintest","sa","sa");sm=ct.createStatement();rs=sm.executeQuery("select*fromuserinfowhereusername='"+name+"'andpassword='"+password+"'");JSONArraysingers=newJSONArray();JSONObjectparams=newJSONObject();if(rs.next()){HttpSessionhs=req.getSession(true);//session失效时间hs.setMaxInactiveInterval(10);hs.setAttribute("pass","ok");//服务器端定义,0为登陆成功,1为登陆失败status=1;params.put("status","0");this.singers.add(params);//resp.sendRedirect("success.jsp?username="+name);falg=true;}else{status=0;params.put("status","1");singers.add(params);resp.sendRedirect("index.jsp");falg=false;}}catch(ClassNotFoundExceptione){e.printStackTrace();}catch(SQLExceptione){e.printStackTrace();}finally{try{if(rs!=null){rs.close();}if(sm!=null){sm.close();}if(ct!=null){ct.close();}}catch(Exceptionex){ex.printStackTrace();}}System.out.println("用户名:"+name+"密码:"+password);}/***@returnthefalg*/publicbooleanisFalg(){returnfalg;}/***@paramfalgthefalgtoset*/publicvoidsetFalg(booleanfalg){this.falg=falg;}publicvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{doGet(req,resp);}Eclipse://下面这行报错,说是空指针JSONObjectjsonObject=newJSONObject().getJSONObject("status");安卓端能连接上服务器,请问是我的获取语法有问题吗?
解决方案
解决方案二:
http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html