1:三种传输方式的分析。
综合以上分析:选择json+httphandler 的传输方式
2 登陆和注册两个接口实现
2.1 登陆接口实现
接口定义:
http://192.168.1.1/gointel/UserH ... ;PassWord=xuwenbing
反馈的json格式:
{ActionResult:false,Reason:""账号和密码不一致""}
01
02
03
04
05
06
07
08
09
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
|
/*用户登陆
* 服务器检测用户的账号和密码是否一致,并返回结果
* */
public
static
Boolean Login(String Account ,String PassWord)
{
//Step One 从服务器接口中获取当前账号和密码的配对情况
Boolean actionResult=
false
;
String httpUrl=
"http://221.181.127.43/gointel/UserHandler.ashx?Action=login&Account="
+Account+
"&PassWord="
+PassWord;
//httpGet 连接对象
HttpGet httpRequest =
new
HttpGet(httpUrl);
try
{
//取得HttpClinet对象
HttpClient httpclient=
new
DefaultHttpClient();
// 请求HttpClient,取得HttpResponse
HttpResponse httpResponse=httpclient.execute(httpRequest);
//请求成功
if
(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK)
{
//取得返回的字符串
String strResult=EntityUtils.toString(httpResponse.getEntity());
JSONObject jsonObject =
new
JSONObject(strResult) ;
//获取返回值,并判断是否正确
actionResult=jsonObject.getBoolean(
"ActionResult"
);
}
}
catch
(Exception e)
{
return
false
;
}
return
actionResult;
}
|
2.2 注册接口实现
接口定义:
反馈json格式:
{ActionResult:false,Reason:""用户名已存在""}
01
02
03
04
05
06
07
08
09
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
|
public
static
Boolean Register(String account, String passWord,
String niceName) {
Boolean actionResult=
false
;
String httpUrl=
"http://221.181.127.43/gointel/UserHandler.ashx?Action=register&Account="
+account+
"&PassWord="
+passWord+
"&NiceName="
+niceName;
//httpGet 连接对象
HttpGet httpRequest =
new
HttpGet(httpUrl);
try
{
//取得HttpClinet对象
HttpClient httpclient=
new
DefaultHttpClient();
// 请求HttpClient,取得HttpResponse
HttpResponse httpResponse=httpclient.execute(httpRequest);
//请求成功
if
(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK)
{
//取得返回的字符串
String strResult=EntityUtils.toString(httpResponse.getEntity());
JSONObject jsonObject =
new
JSONObject(strResult) ;
//获取返回值,并判断是否正确
actionResult=jsonObject.getBoolean(
"ActionResult"
);
}
}
catch
(Exception e)
{
return
false
;
}
return
actionResult;
}
|