From:http://blog.youkuaiyun.com/qq_35114086/article/details/52132034
1:三种传输方式的分析。
综合以上分析:选择json+httphandler 的传输方式
2 登陆和注册两个接口实现
2.1 登陆接口实现
接口定义:
http://192.168.1.1/gointel/UserH ... ;PassWord=xuwenbing
反馈的json格式:
{ActionResult:false,Reason:""账号和密码不一致""}
[mw_shl_code=java,true]
/*用户登陆
* 服务器检测用户的账号和密码是否一致,并返回结果
* */
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;
}[/mw_shl_code]
2.2 注册接口实现
接口定义:
反馈json格式:
{ActionResult:false,Reason:""用户名已存在""}
[mw_shl_code=java,true]
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;
}[/mw_shl_code]
6409



被折叠的 条评论
为什么被折叠?



