您的位置:首页 > 移动开发 > Android开发

关于开发开源软件的授权 及获取数据的分析

2013-09-09 17:54 603 查看
关于 开发开源软件的授权 及 获取数据的分析

     对于开发开源软件,一版情况下该软件的网站都提供了开发文档, 我们仔细的阅读开发文档,便会发现要有授权的这一步操作

  那么我们怎么在代码中实现呢? 今天就来说一下我的理解: (代码如下,以豆瓣网为例,有详细的注释)

public class NetUtil {

public static ArrayList<String> login(String email, String pwd,
String captcha_solution, String captcha_id) {
try {
// 模拟登陆豆瓣网站
String loginpath = "http://www.douban.com/accounts/login";
DefaultHttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(loginpath);
// 提交的数据的实体
// source=simple&redir=http%3A%2F%2Fwww.douban.com%2Fservice%2Fauth%2Fauthorize%3Foauth_token%3D9267a49c1548126db736edd43fd83747

// &form_email=itcastweibo@sina.cn&form_password=a11111&remember=on&user_login=%E7%99%BB%E5%BD%95
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("source", "simple"));
parameters.add(new BasicNameValuePair("redir","http://www.douban.com"));
parameters.add(new BasicNameValuePair("form_email", email));
parameters.add(new BasicNameValuePair("form_password", pwd));
if (captcha_solution != null && captcha_id != null) {
parameters.add(new BasicNameValuePair("captcha-solution",
captcha_solution));
parameters
.add(new BasicNameValuePair("captcha-id", captcha_id));
}

parameters.add(new BasicNameValuePair("remember", "on"));
parameters.add(new BasicNameValuePair("user_login", "登录"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters);
post.setEntity(entity);
HttpResponse response = client.execute(post);
System.out.println("登陆的状态码"+ response.getStatusLine().getStatusCode());
byte[] result = StreamTool.read(response.getEntity().getContent());
System.out.println(new String(result));

// 完成登陆 获取到 cookies

// TODO:模拟用户点击同意授权的操作
// 获取认证的url
// 1.豆瓣官网申请一个 apkkey 还有secret
String apiKey = "06469c2e1ccac8df12d3d48a56605a9d";
String secret = "fb078b49b67c7ad3";
// 2.带着申请的key去访问豆瓣的网站
DoubanService myService = new DoubanService("黑马7的小豆瓣", apiKey,
secret);

String authpath = myService.getAuthorizationUrl(null);
String oauth_token = authpath.substring(
authpath.lastIndexOf("=") + 1, authpath.length());
HttpPost authpost = new HttpPost(authpath);
List<BasicNameValuePair> authparameters = new ArrayList<BasicNameValuePair>();
authparameters.add(new BasicNameValuePair("ck", "T3g9"));
authparameters.add(new BasicNameValuePair("oauth_token",
oauth_token));
authparameters.add(new BasicNameValuePair("oauth_callback", ""));
authparameters.add(new BasicNameValuePair("ssid", "2e646807"));
authparameters.add(new BasicNameValuePair("confirm", "同意"));

UrlEncodedFormEntity authentity = new UrlEncodedFormEntity(
authparameters);
authpost.setEntity(authentity);
HttpResponse authResponse = client.execute(authpost);
System.out.println("登陆的状态码"
+ authResponse.getStatusLine().getStatusCode());
byte[] authresult = StreamTool.read(authResponse.getEntity()
.getContent());
System.out.println(new String(authresult));

// 3.获取豆瓣网站给我们打开的后门的钥匙
ArrayList<String> tokens = myService.getAccessToken();
System.out.println("accesstonken " + tokens.get(0));
System.out.println("tokensecret" + tokens.get(1));

/*
* String id = myService.getAuthorizedUser().getUid(); 
* UserEntry ue= myService.getUser(id); 
* System.out.println("id is " +ue.getId()); 
* System.out.println("uid is " + ue.getUid());
* System.out.println("location is " + ue.getLocation());
* System.out.println("content is " + ((TextContent)ue.getContent()).getContent().getPlainText());
* System.out.println("title is " + ue.getTitle().getPlainText());
*/
return tokens;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

 关于获取数据下面以获取最近更新的书为例:

/**
* 获取豆瓣的所有的新书信息
*/
public static List<NewBook> getNewbooks() throws Exception {
String path = "http://book.douban.com/latest";
URL url = new URL(path);
URLConnection conn = url.openConnection();
Source source = new Source(conn);// 服务器返回回来的html的对象
List<NewBook> newBooks = new ArrayList<NewBook>();
//读取xml
List<Element> liElements = source.getAllElements("li");
for (Element liElement : liElements) {
if (liElement.getChildElements().size() == 2) {
Element firstChilddiv = liElement.getChildElements().get(0);
Element secondChilda = liElement.getChildElements().get(1);

if ("detail-frame".equals(firstChilddiv
.getAttributeValue("class"))) {
NewBook newBook = new NewBook();
List<Element> divchildren = firstChilddiv
.getChildElements();
String title = divchildren.get(0).getTextExtractor()
.toString();
newBook.setTitle(title);
String description = divchildren.get(1).getTextExtractor()
.toString();
newBook.setDescription(description);
String content = divchildren.get(2).getTextExtractor()
.toString();
newBook.setContent(content);

String iconpath = secondChilda.getChildElements().get(0)
.getAttributeValue("src");
newBook.setIconpath(iconpath);
newBooks.add(newBook);
newBook = null;
}
}
}
return newBooks;
}

不足之处望指正,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息