1. 依赖

      1. implementation 'me.luzhuo.java.android:lib_okhttp:1.0.7-SNAPSHOT'
      2. implementation 'com.squareup.okhttp3:okhttp:3.14.9'
    2. 使用

      1. try {
      2. OKHttpManager ok = new OKHttpManager(new TokenInterceptor() {
      3. @Override
      4. public String getToken() {
      5. return "adsad";
      6. }
      7. });
      8. String url = "http://www.baidu.com";
      9. // String message_get = ok.get(url);
      10. ok.get(url, new IContentCallback() {
      11. @Override
      12. public void onSuccess(int code, String data) {
      13. Log.e(TAG, "<<<" + data);
      14. }
      15. @Override
      16. public void onError(int code, String error) {
      17. Toast.makeText(MainActivity.this, "网络异常, 请检查网络!", Toast.LENGTH_SHORT).show();
      18. }
      19. });
      20. // String message_post = ok.post(url, "{}", PostType.JSON);
      21. ok.post(url, "{}", PostType.JSON, new IContentCallback() {
      22. @Override
      23. public void onSuccess(int i, String data) {
      24. Log.e(TAG, ">>>" + data);
      25. }
      26. @Override
      27. public void onError(int i, String error) {
      28. Toast.makeText(MainActivity.this, "网络异常, 请检查网络!", Toast.LENGTH_SHORT).show();
      29. }
      30. });
      31. // Log.e(TAG, "" + message);
      32. } catch (Exception e) {
      33. e.printStackTrace();
      34. }