PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ShareText/youkuLoginSDK/src/main/java/com/youku/login/sns/ServiceShell.java

https://gitlab.com/ShaneNilsson/JarToUnity
Java | 82 lines | 39 code | 11 blank | 32 comment | 3 complexity | ac27f529619182e126d2042e1cdd5986 MD5 | raw file
  1. package com.youku.login.sns;
  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.client.HttpClient;
  4. import org.apache.http.client.methods.HttpPost;
  5. import org.apache.http.client.methods.HttpUriRequest;
  6. import org.apache.http.util.EntityUtils;
  7. import com.alibaba.fastjson.JSON;
  8. import com.youku.login.sns.bean.SinaWeiboToken;
  9. import com.youku.login.sns.util.ConfigUtil;
  10. import com.youku.login.sns.util.HttpClientFactory;
  11. public class ServiceShell {
  12. private ServiceShell() {
  13. }
  14. /**
  15. * 获取授权信息请求
  16. */
  17. public static void getSinaTokenInfo(ServiceShellListener<SinaWeiboToken> serviceShellListener) {
  18. StringBuilder sb = new StringBuilder("https://api.weibo.com/oauth2/access_token?");
  19. sb.append("client_id=" + ConfigUtil.sina_client_id);
  20. sb.append("&client_secret=" + ConfigUtil.sina_client_secret);
  21. sb.append("&grant_type=authorization_code");
  22. sb.append("&code=" + ((LoginBySinaWeibo) ConfigUtil.oauthInter).getCode());
  23. sb.append("&redirect_uri=" + ConfigUtil.sina_redirect_uri);
  24. HttpPost post = new HttpPost(sb.toString());
  25. send(serviceShellListener, post, SinaWeiboToken.class);
  26. }
  27. // /**
  28. // * 获取用户的信息
  29. // * */
  30. // public static void getWeiboUserInfo(String access_token, ServiceShellListener<UserSM> serviceShellListener) {
  31. // StringBuilder sb = new StringBuilder("https://api.weibo.com/2/users/show.json?");
  32. // try {
  33. // sb.append("access_token=" + access_token + "&screen_name=" + URLEncoder.encode("披沙拣金", HTTP.UTF_8));
  34. // } catch (UnsupportedEncodingException e) {
  35. // // TODO Auto-generated catch block
  36. // e.printStackTrace();
  37. // }
  38. // HttpGet post = new HttpGet(sb.toString());
  39. // /*
  40. // * 有关Content-Type属性值可以如下两种编码类型: (1)“application/x-www-form-urlencoded”:
  41. // * 表单数据向服务器提交时所采用的编码类型, 默认的缺省值就是“application/x-www-form-urlencoded”。
  42. // * 然而,在向服务器发送大量的文本、包含非ASCII字符的文本或二进制数据时这种编码方式效率很低。
  43. // * (2)“multipart/form-data”: 在文件上载时,所使用的编码类型应当是“multipart/form-data”,
  44. // * 它既可以发送文本数据,也支持二进制数据上载。 当提交为单单数据时,
  45. // * 可以使用“application/x-www-form-urlencoded”;
  46. // * 当提交的是文件时,就需要使用“multipart/form-data”编码类型。
  47. // * 在Content-Type属性当中还是指定提交内容的charset字符编码。
  48. // * 一般不进行设置,它只是告诉web服务器post提交的数据采用的何种字符编码。
  49. // */
  50. // post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
  51. // send(serviceShellListener, post, UserSM.class);
  52. // }
  53. /**
  54. * 发送请求
  55. * */
  56. @SuppressWarnings("unchecked")
  57. private static void send(ServiceShellListener listenner, HttpUriRequest request, Class clazz) {
  58. try {
  59. HttpClient httpClient = HttpClientFactory.getInstance();
  60. HttpResponse response = httpClient.execute(request);
  61. if (response.getStatusLine().getStatusCode() == 200) {
  62. String msg = EntityUtils.toString(response.getEntity(), "UTF-8");
  63. listenner.completed(JSON.parseObject(msg, clazz));
  64. } else {
  65. listenner.failed("网络问题");
  66. }
  67. } catch (Exception e) {
  68. listenner.failed(e.getMessage());
  69. }
  70. }
  71. }