高校手机签到系统——手机客户端
先前写了服务器端的权限系统:
高校手机签到系统——第一部分Authority权限系统(上)
高校手机签到系统——第一部分Authority权限系统(下)
一、服务器端说明
服务器端还有选课系统:
课程表:
在技术上,它们和权限系统相比没有更多的可以分享。接着来说说客户端。
二、客户端
客户端的难点就是和服务器端进行通信,很显然,服务器端是.net平台,客户端是android java平台,怎么使他们之间进行数据交换呢?这里有两种方法,当然是走协议,一种soap协议,一种httpclient模拟浏览器发出http请求。两张方法在我的客户端都有体现,先说说第一种,举个例子,这是一个客户端向服务器端发送一个string,这个string是从二维码图片上扫描到的某课程的唯一标识,发送到服务器端以完成签到。public class SignInThread implements Runnable { public SignInThread() { } @Override public void run() { String targetNameSpace="http://tempuri.org/"; String WSDL=stringThings.getServerURL()+"/Models/SignInService.svc"; //String WSDL="http://10.0.2.2:1034/Models/SignInService.svc"; String MessageName="SignInRequest"; String SoapAction="http://tempuri.org/ISignInService/SignIn"; SoapObject soapObject=new SoapObject(targetNameSpace,MessageName); PropertyInfo pi = new PropertyInfo(); SignEntry signEntry=new SignEntry(); signEntry.setSignId(signId); pi.setName("SignEntry"); pi.setValue(signEntry); pi.setType(SignEntry.class); soapObject.addProperty(pi); Element[] header = new Element[3]; header[0] = new Element().createElement(targetNameSpace, "Username"); header[0].addChild(Node.TEXT, stringThings.getUsername()); header[1] = new Element().createElement(targetNameSpace, "IMSI"); header[1].addChild(Node.TEXT, stringThings.getIMSI()); header[2] = new Element().createElement(targetNameSpace, "IMEI"); header[2].addChild(Node.TEXT, stringThings.getIMEI()); SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.addMapping(SignEntry.NAMESPACE,"SignEntry",SignEntry.class);//register 这个很重要 envelope.dotNet=true; envelope.headerOut = header; envelope.setOutputSoapObject(soapObject); HttpTransportSE httpTranstation=new HttpTransportSE(WSDL); try { httpTranstation.call(SoapAction, envelope); SoapPrimitive response =(SoapPrimitive)envelope.getResponse(); if(response.toString()=="true") CaptureActivity.this.finish(); } catch (SoapFault soapFault) { soapFault.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. CaptureActivity.this.failToSign(); } catch (HttpResponseException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. CaptureActivity.this.failToSign(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. CaptureActivity.this.failToSign(); } catch (XmlPullParserException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. CaptureActivity.this.failToSign(); } } }
上面的方法中,有三个soapheader和下面C#代码对应:
[MessageContract] public class SignInRequest { [MessageHeader] public string Username { get; set; } [MessageHeader] public string IMSI { get; set; } [MessageHeader] public string IMEI { get; set; } [MessageBodyMember] public SignEntry SignEntry { get; set; } public int IsUsersMessage() { UserSvc userSvc = new UserSvcImpl(); Profile user = userSvc.SelectByName<Profile>(Username)[0]; if (user.IMSI == "" | user.IMEI == "") return -1; else { if (user.IMSI == IMSI && user.IMEI == IMEI) return 1; else return 0; } } } [MessageContract] public class SignInResponse { [MessageBodyMember] public bool IsSignIn { get; set; } }
上面ksoap调用服务器端的时候出现了一个异常WebSessionStore: Could not obtain reference to HttpContext,解决方法是
加上这句代码sqlMapper.SessionStore = new HybridWebThreadSessionStore(sqlMapper.Id);
第二种httpclient,在post和get上略有不同:
post方法
get方法
public class LoadScheduleThread implements Runnable //获取课程表,采用post方法 { public LoadScheduleThread() { } @Override public void run() { HttpClient httpClient=new DefaultHttpClient(); String targetUrl =stringThings.getServerURL()+"/Schedule/QuerySchedule"; //String targetUrl ="http://10.0.2.2:1034/Schedule/QuerySchedule"; HttpPost httpPost = new HttpPost(targetUrl); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy"); String date = simpleDateFormat.format(new java.util.Date()); List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); params.add(new BasicNameValuePair("username",stringThings.getUsername())); params.add(new BasicNameValuePair("date",date)); //params.add(new BasicNameValuePair("username","liuxiaomin")); //params.add(new BasicNameValuePair("date","03/11/2014")); try { httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //httpClient.getParams().setParameter("http.connection.timeout",30000); //httpClient.getParams().setParameter("http.socket.timeout", 10000); HttpResponse httpResponse=httpClient.execute(httpPost); String json= EntityUtils.toString(httpResponse.getEntity(), "utf-8"); json="{\"schedule\":"+json+"}"; JSONObject jsonObject= new JSONObject(json); jsonArray = jsonObject.getJSONArray("schedule"); ScheduleActivity.this.loadSchedule(); } catch (JSONException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. ScheduleActivity.this.showLoadScheduleFailure(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. ScheduleActivity.this.showLoadScheduleFailure(); } } }
public class ShowCourseInfoThread implements Runnable //获得某节课的信息,采用get方法 { public ShowCourseInfoThread() { } @Override public void run() { HttpClient httpClient=new DefaultHttpClient(); String targetUrl =stringThings.getServerURL()+"/Schedule/QueryScheduleById"; //String targetUrl ="http://10.0.2.2:1034/Schedule/QueryScheduleById"; List<Map<String, Object>> list=getCourses(); Map<String,Object> map=list.get(index); List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); params.add(new BasicNameValuePair("id",map.get("id").toString())); try { HttpGet httpGet = new HttpGet(targetUrl+"?"+ URLEncodedUtils.format(params,"utf-8")); HttpResponse httpResponse=httpClient.execute(httpGet); String json= EntityUtils.toString(httpResponse.getEntity(), "utf-8"); json="{\"teacher\":"+json+"}"; JSONObject jsonObject= new JSONObject(json).getJSONObject("teacher"); teacherName=jsonObject.getString("ManagerName"); ScheduleActivity.this.showCourseInfo(); } catch (JSONException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. ScheduleActivity.this.showCourseInfoFailure(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. ScheduleActivity.this.showCourseInfoFailure(); } } }