net_work.png

  • content_work: 文本内容类网络请求
    • OKHttpManager: 文本网络请求
  • file_work: 文件类网络请求
    • ALiOSSFileServer: AliOSS文件服务

1. OKHttpManager 使用

  1. OKHttpManager manager = new OKHttpManager(
  2. new RequestTimeInterceptor() {
  3. public void requestTIme(String requestUrl, int requestTime) {
  4. Log.e(TAG, "" + requestUrl + " : " + requestTime);
  5. }
  6. },
  7. new TokenInterceptor() {
  8. public String getToken() {
  9. return "66666666";
  10. }
  11. }
  12. );
  13. // sync post
  14. String data = manager.post(url, "666", PostType.JSON);
  15. // async post
  16. manager.post(url, "666", PostType.JSON, new IContentCallback() {
  17. public void onSuccess(int code, String data) {
  18. Log.e(TAG, "" + code + " : " + data);
  19. }
  20. public void onError(int code, String error) {
  21. Log.e(TAG, "" + code + " : " + error);
  22. }
  23. });

2. ALiOSSFileServer 使用

  1. private static final String FILE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
  2. public void onclick(View view) {
  3. String STSServer = "https://192.168.10.124:7080/getAliToekn";
  4. String EndPoint = "cj-oss.save.xyz";
  5. String BucketName = "databucket";
  6. String prefix = "666/";
  7. ALiOSSFileServer aLiOSSFileServer = new ALiOSSFileServer(this, STSServer, EndPoint, BucketName, prefix){
  8. @Override
  9. public OSSFederationToken getTokenFromUser(String STSServer) {
  10. try {
  11. // Get temporary token.
  12. OKHttpManager manager = new OKHttpManager(new TokenInterceptor() {
  13. @Override
  14. public String getToken() {
  15. return "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzMzE2MyIsImlhdCI6MTU4OTYxNDczNSwiZXhwIjoxNTkwMjE5NTM1fQ.DyuUllLfRAmv85OY2e9pjME1jQ2o44tsSu4cr4iRVwYljg6y9gml_f3h0ztEsrtBAw_y3ztk3QhjzuiIpAIUmw";
  16. }
  17. });
  18. String message = manager.get(STSServer);
  19. Gson gson = new Gson();
  20. STSBean bean = gson.fromJson(message, STSBean.class);
  21. Log.e(TAG, "" + bean);
  22. if(bean.code != 200){
  23. return null;
  24. }
  25. STSBean.DataBean dataBean = bean.data;
  26. Log.e(TAG, "" + dataBean);
  27. return new OSSFederationToken(dataBean.AccessKeyId, dataBean.AccessKeySecret, dataBean.SecurityToken, dataBean.Expiration);
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. } catch (NoSuchAlgorithmException e) {
  31. e.printStackTrace();
  32. } catch (KeyManagementException e) {
  33. e.printStackTrace();
  34. } catch (NetErrorException e) {
  35. e.printStackTrace();
  36. }
  37. return null;
  38. }
  39. };
  40. // Single file upload
  41. FileBean fileBean = new FileBean();
  42. fileBean.setPath2AliOSS(FILE_DIR + "wangwang.zip", UUID.randomUUID().toString().replace("-",""));
  43. aLiOSSFileServer.uploadFileByAliOSS(fileBean, new IProgress() {
  44. @Override
  45. public void onProgress(int progress, long currentSize, long totalSize) {
  46. Log.e(TAG, "" + progress + " : " + currentSize + " : " + totalSize);
  47. }
  48. }, new FileWorkCallbackImpl() {
  49. @Override
  50. public void onSuccess(FileBean fileBean) {
  51. Log.e(TAG, "" + fileBean);
  52. }
  53. @Override
  54. public void onError(String errorMessage) {
  55. Log.e(TAG, "" + errorMessage);
  56. }
  57. });
  58. // Multi-file upload
  59. List<FileBean> files = new ArrayList<>();
  60. for (int i = 1; i < 10; i++) {
  61. FileBean fileBean2 = new FileBean();
  62. fileBean2.setPath2AliOSS(FILE_DIR + "wangwang.zip", UUID.randomUUID().toString().replace("-",""));
  63. files.add(fileBean2);
  64. }
  65. aLiOSSFileServer.uploadFilesByAliOSS(files, new IProgress() {
  66. @Override
  67. public void onProgress(int progress, long currentSize, long totalSize) {
  68. Log.e(TAG, "" + progress);
  69. }
  70. }, new FileWorkCallbackImpl(){
  71. @Override
  72. public void onSuccess(List<FileBean> networkUrls) {
  73. for (FileBean fileBean : networkUrls) {
  74. Log.e(TAG, "" + fileBean.getPath2AliOSS());
  75. }
  76. }
  77. @Override
  78. public void onError(String errorMessage) {
  79. Log.e(TAG, "" + errorMessage);
  80. }
  81. });
  82. }