实现嘀咕网验证登录

下面这段代码就是一个初步实现嘀咕网验证登录的过程,登录成功后会显示用户的四个基本信息我这里请求的是一个xml数据,具体请看API,并且这里cbin的密码被我以为xxxx代替了,建议大家自己去注册一个嘀咕网账户,我这里是通过服务器返回的状态码直接判断是否登录成功,在用kirin那种方式的时候,假如是用户名和密码错误会报一个异常,也就是严格来说它的登录还是有点问题的,我这种方式就是凭我个人对于Android之基于HTTP协议的下载 中的Status code等于401的理解,不知道这样处理是否合理。

代码
private boolean LoginDidu(){
String spec
="http://api.minicloud.com.cn/account/verify.xml?isAllInfo=false";
try {
URL url
=new URL(spec);
HttpURLConnection connect
=(HttpURLConnection)url.openConnection();
//设置读取服务器资源权限
connect.setDoOutput(true);
//设置连接服务器超时时长
connect.setConnectTimeout(10*1000);
//设置从服务器读取资源超时时长
connect.setReadTimeout(30*1000);
//设置请求方法
connect.setRequestMethod("GET");
//Authorization请求报头域主要用于证明客户端有权查看某个资源
//用于嘀咕网用户认证(详情请看嘀咕API:http://code.google.com/p/digu-api/wiki/DiguApi
String user="cbin:xxxx";
connect.setRequestProperty(
"Authorization", "Basic "+Base64.encodeBytes(user.getBytes()));
int code=connect.getResponseCode();
Log.v(tag,
"code----------"+code);
if(code>=0 && code<299){
connect.connect();
InputStream is
=connect.getInputStream();
InputStreamReader isr
=new InputStreamReader(is, "utf-8");
BufferedReader br
=new BufferedReader(isr);
StringBuffer sb
=new StringBuffer();
String readLine
=null;
while((readLine=br.readLine())!=null){
sb.append(readLine).append(
"\n");
}
showText
=new String(sb.toString().getBytes("utf-8"), "utf-8");
return true;
}
else if(code==401){
//服务器返回状态码如果等于401表示授权未通过(也就是登录失败)
return false;
}
Log.v(tag,
"code-----------"+code);
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
posted @ 2011-08-11 16:09  since1987  阅读(648)  评论(0)    收藏  举报