先前写了服务器端的权限系统:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 |
public
class SignInThread implements
Runnable { public
SignInThread() { } @Override public
void run() { String WSDL=stringThings.getServerURL()+ "/Models/SignInService.svc" ; //String WSDL="http://10.0.2.2:1034/Models/SignInService.svc"; String MessageName= "SignInRequest" ; 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#代码对应:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 |
[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上略有不同:
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(); } } }
下面是我的客户端的截图:
最后一张扫描的是服务器端生成的二维码:
好了,手机签到系统到此完成。
高校手机签到系统——手机客户端,布布扣,bubuko.com
原文:http://www.cnblogs.com/DKSL/p/android_ksoap2_httpclient.html