android快速开发--常用utils类

  1. * @param message

  2. */

  3. public static void showShort(Context context, CharSequence message)

  4. {

  5. if (isShow)

  6. Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

  7. }

  8. /**

  9. * 短时间显示Toast

  10. *

  11. * @param context

  12. * @param message

  13. */

  14. public static void showShort(Context context, int message)

  15. {

  16. if (isShow)

  17. Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

  18. }

  19. /**

  20. * 长时间显示Toast

  21. *

  22. * @param context

  23. * @param message

  24. */

  25. public static void showLong(Context context, CharSequence message)

  26. {

  27. if (isShow)

  28. Toast.makeText(context, message, Toast.LENGTH_LONG).show();

  29. }

  30. /**

  31. * 长时间显示Toast

  32. *

  33. * @param context

  34. * @param message

  35. */

  36. public static void showLong(Context context, int message)

  37. {

  38. if (isShow)

  39. Toast.makeText(context, message, Toast.LENGTH_LONG).show();

  40. }

  41. /**

  42. * 自定义显示Toast时间

  43. *

  44. * @param context

  45. * @param message

  46. * @param duration

  47. */

  48. public static void show(Context context, CharSequence message, int duration)

  49. {

  50. if (isShow)

  51. Toast.makeText(context, message, duration).show();

80
.     }

  1. /**

  2. * 自定义显示Toast时间

  3. *

  4. * @param context

  5. * @param message

  6. * @param duration

  7. */

  8. public static void show(Context context, int message, int duration)

  9. {

  10. if (isShow)

  11. Toast.makeText(context, message, duration).show();

  12. }

  13. }

也是非常简单的一个封装,能省则省了~~

3、SharedPreferences封装类SPUtils

===========================================================================================

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. package com.zhy.utils;

  2. import java.lang.reflect.InvocationTargetException;

  3. import java.lang.reflect.Method;

  4. import java.util.Map;

  5. import android.content.Context;

  6. import android.content.SharedPreferences;

  7. public class SPUtils

  8. {

  9. /**

  10. * 保存在手机里面的文件名

  11. */

  12. public static final String FILE_NAME = “share_data”;

  13. /**

  14. * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法

  15. *

  16. * @param context

  17. * @param key

  18. * @param object

  19. */

  20. public static void put(Context context, String key, Object object)

  21. {

  22. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,

  23. Context.MODE_PRIVATE);

  24. SharedPreferences.Editor editor = sp.edit();

  25. if (object instanceof String)

  26. {

  27. editor.putString(key, (String) object);

  28. } else if (object instanceof Integer)

  29. {

  30. editor.putInt(key, (Integer) object);

  31. } else if (object instanceof Boolean)

  32. {

  33. editor.putBoolean(key, (Boolean) object);

  34. } else if (object instanceof Float)

  35. {

  36. editor.putFloat(key, (Float) object);

  37. } else if (object instanceof Long)

  38. {

  39. editor.putLong(key, (Long) object);

  40. } else

  41. {

  42. editor.putString(key, object.toString());

  43. }

  44. SharedPreferencesCompat.apply(editor);

  45. }

  46. /**

  47. * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值

  48. *

  49. * @param context

  50. * @param key

  51. * @param defaultObject

  52. * @return

  53. */

  54. public static Object get(Context context, String key, Object defaultObject)

  55. {

  56. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,

  57. Context.MODE_PRIVATE);

  58. if (defaultObject instanceof String)

  59. {

  60. return sp.getString(key, (String) defaultObject);

  61. } else if (defaultObject instanceof Integer)

  62. {

  63. return sp.getInt(key, (Integer) defaultObject);

  64. } else if (defaultObject instanceof Boolean)

  65. {

  66. return sp.getBoolean(key, (Boolean) defaultObject);

  67. } else if (defaultObject instanceof Float)

  68. {

  69. return sp.getFloat(key, (Float) defaultObject);

  70. } else if (defaultObject instanceof Long)

  71. {

  72. return sp.getLong(key, (Long) defaultObject);

  73. }

  74. return null;

  75. }

  76. /**

  77. * 移除某个key值已经对应的值

  78. * @param context

  79. * @param key

  80. */

  81. public static void remove(Context context, String key)

  82. {

  83. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,

  84. Context.MODE_PRIVATE);

  85. SharedPreferences.Editor editor = sp.edit();

  86. editor.remove(key);

  87. SharedPreferencesCompat.apply(editor);

  88. }

  89. /**

  90. * 清除所有数据

  91. * @param context

  92. */

  93. public static void clear(Context context)

  94. {

  95. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,

  96. Context.MODE_PRIVATE);

  97. SharedPreferences.Editor editor = sp.edit();

  98. editor.clear();

  99. SharedPreferencesCompat.apply(editor);

  100. }

  101. /**

  102. * 查询某个key是否已经存在

  103. * @param context

  104. * @param key

  105. * @return

  106. */

  107. public static boolean contains(Context context, String key)

  108. {

  109. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,

  110. Context.MODE_PRIVATE);

  111. return sp.contains(key);

  112. }

  113. /**

  114. * 返回所有的键值对

  115. *

  116. * @param context

  117. * @return

  118. */

  119. public static Map<String, ?> getAll(Context context)

  120. {

  121. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,

  122. Context.MODE_PRIVATE);

  123. return sp.getAll();

  124. }

  125. /**

  126. * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类

  127. *

  128. * @author zhy

  129. *

  130. */

  131. private static class SharedPreferencesCompat

  132. {

  133. private static final Method sApplyMethod = findApplyMethod();

  134. /**

  135. * 反射查找apply的方法

  136. *

  137. * @return

  138. */

  139. @SuppressWarnings({ “unchecked”, “rawtypes” })

  140. private static Method findApplyMethod()

  141. {

  142. try

  143. {

  144. Class clz = SharedPreferences.Editor.class;

  145. return clz.getMethod(“apply”);

  146. } catch (NoSuchMethodException e)

  147. {

  148. }

  149. return null;

  150. }

  151. /**

  152. * 如果找到则使用apply执行,否则使用commit

  153. *

  154. * @param editor

  155. */

  156. public static void apply(SharedPreferences.Editor editor)

  157. {

  158. try

  159. {

  160. if (sApplyMethod != null)

  161. {

  162. sApplyMethod.invoke(editor);

  163. return;

  164. }

  165. } catch (IllegalArgumentException e)

  166. {

  167. } catch (IllegalAccessException e)

  168. {

  169. } catch (InvocationTargetException e)

  170. {

  171. }

  172. editor.commit();

  173. }

  174. }

  175. }

对SharedPreference的使用做了建议的封装,对外公布出put,get,remove,clear等等方法;

注意一点,里面所有的commit操作使用了SharedPreferencesCompat.apply进行了替代,目的是尽可能的使用apply代替commit

首先说下为什么,因为commit方法是同步的,并且我们很多时候的commit操作都是UI线程中,毕竟是IO操作,尽可能异步;

所以我们使用apply进行替代,apply异步的进行写入;

但是apply相当于commit来说是new API呢,为了更好的兼容,我们做了适配;

SharedPreferencesCompat也可以给大家创建兼容类提供了一定的参考~~

4、单位转换类 DensityUtils

==================================================================================

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. package com.zhy.utils;

  2. import android.content.Context;

  3. import android.util.TypedValue;

  4. /**

  5. * 常用单位转换的辅助类

  6. *

  7. *

  8. *

  9. */

  10. public class DensityUtils

  11. {

  12. private DensityUtils()

  13. {

  14. /* cannot be instantiated */

  15. throw new UnsupportedOperationException(“cannot be instantiated”);

  16. }

  17. /**

  18. * dp转px

  19. *

  20. * @param context

  21. * @param val

  22. * @return

  23. */

  24. public static int dp2px(Context context, float dpVal)

  25. {

  26. return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,

  27. dpVal, context.getResources().getDisplayMetrics());

  28. }

  29. /**

  30. * sp转px

  31. *

  32. * @param context

  33. * @param val

  34. * @return

  35. */

  36. public static int sp2px(Context context, float spVal)

  37. {

  38. return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,

  39. spVal, context.getResources().getDisplayMetrics());

  40. }

  41. /**

  42. * px转dp

  43. *

  44. * @param context

  45. * @param pxVal

  46. * @return

  47. */

  48. public static float px2dp(Context context, float pxVal)

  49. {

  50. final float scale = context.getResources().getDisplayMetrics().density;

  51. return (pxVal / scale);

  52. }

  53. /**

  54. * px转sp

  55. *

  56. * @param fontScale

  57. * @param pxVal

  58. * @return

  59. */

  60. public static float px2sp(Context context, float pxVal)

  61. {

  62. return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);

  63. }

  64. }

5、SD卡相关辅助类 SDCardUtils

====================================================================================

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. package com.zhy.utils;

  2. import java.io.File;

  3. import android.os.Environment;

  4. import android.os.StatFs;

  5. /**

  6. * SD卡相关的辅助类

  7. *

  8. *

  9. *

  10. */

  11. public class SDCardUtils

  12. {

  13. private SDCardUtils()

  14. {

  15. /* cannot be instantiated */

  16. throw new UnsupportedOperationException(“cannot be instantiated”);

  17. }

  18. /**

  19. * 判断SDCard是否可用

  20. *

  21. * @return

  22. */

  23. public static boolean isSDCardEnable()

  24. {

  25. return Environment.getExternalStorageState().equals(

  26. Environment.MEDIA_MOUNTED);

  27. }

  28. /**

  29. * 获取SD卡路径

  30. *

  31. * @return

  32. */

  33. public static String getSDCardPath()

  34. {

  35. return Environment.getExternalStorageDirectory().getAbsolutePath()

  36. + File.separator;

  37. }

  38. /**

  39. * 获取SD卡的剩余容量 单位byte

  40. *

  41. * @return

  42. */

  43. public static long getSDCardAllSize()

  44. {

  45. if (isSDCardEnable())

  46. {

  47. StatFs stat = new StatFs(getSDCardPath());

  48. // 获取空闲的数据块的数量

  49. long availableBlocks = (long) stat.getAvailableBlocks() - 4;

  50. // 获取单个数据块的大小(byte)

  51. long freeBlocks = stat.getAvailableBlocks();

  52. return freeBlocks * availableBlocks;

  53. }

  54. return 0;

  55. }

  56. /**

  57. * 获取指定路径所在空间的剩余可用容量字节数,单位byte

  58. *

  59. * @param filePath

  60. * @return 容量字节 SDCard可用空间,内部存储可用空间

  61. */

  62. public static long getFreeBytes(String filePath)

  63. {

  64. // 如果是sd卡的下的路径,则获取sd卡可用容量

  65. if (filePath.startsWith(getSDCardPath()))

  66. {

  67. filePath = getSDCardPath();

  68. } else

  69. {// 如果是内部存储的路径,则获取内存存储的可用容量

  70. filePath = Environment.getDataDirectory().getAbsolutePath();

  71. }

  72. StatFs stat = new StatFs(filePath);

  73. long availableBlocks = (long) stat.getAvailableBlocks() - 4;

  74. return stat.getBlockSize() * availableBlocks;

  75. }

  76. /**

  77. * 获取系统存储路径

  78. *

  79. * @return

  80. */

  81. public static String getRootDirectoryPath()

  82. {

  83. return Environment.getRootDirectory().getAbsolutePath();

  84. }

  85. }

6、屏幕相关辅助类 ScreenUtils

===================================================================================

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. package com.zhy.utils;

  2. import android.app.Activity;

  3. import android.content.Context;

  4. import android.graphics.Bitmap;

  5. import android.graphics.Rect;

  6. import android.util.DisplayMetrics;

  7. import android.view.View;

  8. import android.view.WindowManager;

  9. /**

  10. * 获得屏幕相关的辅助类

  11. *

  12. *

  13. *

  14. */

  15. public class ScreenUtils

  16. {

  17. private ScreenUtils()

  18. {

  19. /* cannot be instantiated */

  20. throw new UnsupportedOperationException(“cannot be instantiated”);

  21. }

  22. /**

  23. * 获得屏幕高度

  24. *

  25. * @param context

  26. * @return

  27. */

  28. public static int getScreenWidth(Context context)

  29. {

  30. WindowManager wm = (WindowManager) context

  31. .getSystemService(Context.WINDOW_SERVICE);

  32. DisplayMetrics outMetrics = new DisplayMetrics();

  33. wm.getDefaultDisplay().getMetrics(outMetrics);

  34. return outMetrics.widthPixels;

  35. }

  36. /**

  37. * 获得屏幕宽度

  38. *

  39. * @param context

  40. * @return

  41. */

  42. public static int getScreenHeight(Context context)

  43. {

  44. WindowManager wm = (WindowManager) context

  45. .getSystemService(Context.WINDOW_SERVICE);

  46. DisplayMetrics outMetrics = new DisplayMetrics();

  47. wm.getDefaultDisplay().getMetrics(outMetrics);

  48. return outMetrics.heightPixels;

  49. }

  50. /**

  51. * 获得状态栏的高度

  52. *

  53. * @param context

  54. * @return

  55. */

  56. public static int getStatusHeight(Context context)

  57. {

  58. int statusHeight = -1;

  59. try

  60. {

  61. Class<?> clazz = Class.forName(“com.android.internal.R$dimen”);

  62. Object object = clazz.newInstance();

  63. int height = Integer.parseInt(clazz.getField(“status_bar_height”)

  64. .get(object).toString());

  65. statusHeight = context.getResources().getDimensionPixelSize(height);

  66. } catch (Exception e)

  67. {

  68. e.printStackTrace();

  69. }

  70. return statusHeight;

  71. }

  72. /**

  73. * 获取当前屏幕截图,包含状态栏

  74. *

  75. * @param activity

  76. * @return

  77. */

  78. public static Bitmap snapShotWithStatusBar(Activity activity)

  79. {

  80. View view = activity.getWindow().getDecorView();

  81. view.setDrawingCacheEnabled(true);

  82. view.buildDrawingCache();

  83. Bitmap bmp = view.getDrawingCache();

  84. int width = getScreenWidth(activity);

  85. int height = getScreenHeight(activity);

  86. Bitmap bp = null;

  87. bp = Bitmap.createBitmap(bmp, 0, 0, width, height);

  88. view.destroyDrawingCache();

  89. return bp;

  90. }

  91. /**

  92. * 获取当前屏幕截图,不包含状态栏

  93. *

  94. * @param activity

  95. * @return

  96. */

  97. public static Bitmap snapShotWithoutStatusBar(Activity activity)

  98. {

  99. View view = activity.getWindow().getDecorView();

  100. view.setDrawingCacheEnabled(true);

  101. view.buildDrawingCache();

  102. Bitmap bmp = view.getDrawingCache();

  103. Rect frame = new Rect();

  104. activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

  105. int statusBarHeight = frame.top;

  106. int width = getScreenWidth(activity);

  107. int height = getScreenHeight(activity);

  108. Bitmap bp = null;

  109. bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height

  110. - statusBarHeight);

  111. view.destroyDrawingCache();

  112. return bp;

  113. }

  114. }

7、App相关辅助类

========================================================================

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. package com.zhy.utils;

  2. import android.content.Context;

  3. import android.content.pm.PackageInfo;

  4. import android.content.pm.PackageManager;

  5. import android.content.pm.PackageManager.NameNotFoundException;

  6. /**

  7. * 跟App相关的辅助类

  8. *

  9. *

  10. *

  11. */

  12. public class AppUtils

  13. {

  14. private AppUtils()

  15. {

  16. /* cannot be instantiated */

  17. throw new UnsupportedOperationException(“cannot be instantiated”);

  18. }

  19. /**

  20. * 获取应用程序名称

  21. */

  22. public static String getAppName(Context context)

  23. {

  24. try

  25. {

  26. PackageManager packageManager = context.getPackageManager();

  27. PackageInfo packageInfo = packageManager.getPackageInfo(

  28. context.getPackageName(), 0);

  29. int labelRes = packageInfo.applicationInfo.labelRes;

  30. return context.getResources().getString(labelRes);

  31. } catch (NameNotFoundException e)

  32. {

  33. e.printStackTrace();

  34. }

  35. return null;

  36. }

  37. /**

  38. * [获取应用程序版本名称信息]

  39. *

  40. * @param context

  41. * @return 当前应用的版本名称

  42. */

  43. public static String getVersionName(Context context)

  44. {

  45. try

  46. {

  47. PackageManager packageManager = context.getPackageManager();

  48. PackageInfo packageInfo = packageManager.getPackageInfo(

  49. context.getPackageName(), 0);

  50. return packageInfo.versionName;

  51. } catch (NameNotFoundException e)

  52. {

  53. e.printStackTrace();

  54. }

  55. return null;

  56. }

  57. }

8、软键盘相关辅助类KeyBoardUtils

=====================================================================================

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. package com.zhy.utils;

  2. import android.content.Context;

  3. import android.view.inputmethod.InputMethodManager;

  4. import android.widget.EditText;

  5. /**

  6. * 打开或关闭软键盘

  7. *

  8. * @author zhy

  9. *

  10. */

  11. public class KeyBoardUtils

  12. {

  13. /**

  14. * 打卡软键盘

  15. *

  16. * @param mEditText

  17. *            输入框

  18. * @param mContext

  19. *            上下文

  20. */

  21. public static void openKeybord(EditText mEditText, Context mContext)

  22. {

  23. InputMethodManager imm = (InputMethodManager) mContext

  24. .getSystemService(Context.INPUT_METHOD_SERVICE);

  25. imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);

  26. imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,

  27. InputMethodManager.HIDE_IMPLICIT_ONLY);

  28. }

  29. /**

  30. * 关闭软键盘

  31. *

  32. * @param mEditText

  33. *            输入框

  34. * @param mContext

  35. *            上下文

  36. */

  37. public static void closeKeybord(EditText mEditText, Context mContext)

  38. {

  39. InputMethodManager imm = (InputMethodManager) mContext

  40. .getSystemService(Context.INPUT_METHOD_SERVICE);

  41. imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);

  42. }

  43. }

9、网络相关辅助类 NetUtils

================================================================================

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. package com.zhy.utils;

  2. import android.app.Activity;

  3. import android.content.ComponentName;

  4. import android.content.Context;

  5. import android.content.Intent;

  6. import android.net.ConnectivityManager;

  7. import android.net.NetworkInfo;

  8. /**

  9. * 跟网络相关的工具类

  10. *

  11. *

  12. *

  13. */

  14. public class NetUtils

  15. {

  16. private NetUtils()

  17. {

  18. /* cannot be instantiated */

  19. throw new UnsupportedOperationException(“cannot be instantiated”);

  20. }

  21. /**

  22. * 判断网络是否连接

  23. *

  24. * @param context

  25. * @return

  26. */

  27. public static boolean isConnected(Context context)

  28. {

  29. ConnectivityManager connectivity = (ConnectivityManager) context

  30. .getSystemService(Context.CONNECTIVITY_SERVICE);

  31. if (null != connectivity)

  32. {

  33. NetworkInfo info = connectivity.getActiveNetworkInfo();

  34. if (null != info && info.isConnected())

  35. {

  36. if (info.getState() == NetworkInfo.State.CONNECTED)

  37. {

  38. return true;

  39. }

  40. }

  41. }

  42. return false;

  43. }

  44. /**

  45. * 判断是否是wifi连接

  46. */

  47. public static boolean isWifi(Context context)

  48. {

  49. ConnectivityManager cm = (ConnectivityManager) context

  50. .getSystemService(Context.CONNECTIVITY_SERVICE);

  51. if (cm == null)

  52. return false;

  53. return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;

  54. }

  55. /**

  56. * 打开网络设置界面

  57. */

  58. public static void openSetting(Activity activity)

  59. {

  60. Intent intent = new Intent(“/”);

  61. ComponentName cm = new ComponentName(“com.android.settings”,

  62. “com.android.settings.WirelessSettings”);

  63. intent.setComponent(cm);

  64. intent.setAction(“android.intent.action.VIEW”);

  65. activity.startActivityForResult(intent, 0);

  66. }

  67. }

10、Http相关辅助类 HttpUtils

====================================================================================

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. package com.zhy.utils;

  2. import java.io.BufferedReader;

  3. import java.io.ByteArrayOutputStream;

  4. import java.io.IOException;

  5. import java.io.InputStream;

  6. import java.io.InputStreamReader;

  7. import java.io.PrintWriter;

  8. import java.net.HttpURLConnection;

  9. import java.net.URL;

  10. /**

  11. * Http请求的工具类

  12. *

  13. * @author zhy

  14. *

  15. */

  16. public class HttpUtils

  17. {

  18. private static final int TIMEOUT_IN_MILLIONS = 5000;

  19. public interface CallBack

  20. {

  21. void onRequestComplete(String result);

  22. }

  23. /**

  24. * 异步的Get请求

  25. *

  26. * @param urlStr

  27. * @param callBack

  28. */

  29. public static void doGetAsyn(final String urlStr, final CallBack callBack)

  30. {

  31. new Thread()

  32. {

  33. public void run()

  34. {

  35. try

  36. {

  37. String result = doGet(urlStr);

  38. if (callBack != null)

  39. {

  40. callBack.onRequestComplete(result);

  41. }

  42. } catch (Exception e)

  43. {

  44. e.printStackTrace();

  45. }

  46. };

  47. }.start();

  48. }

  49. /**

  50. * 异步的Post请求

  51. * @param urlStr

  52. * @param params

  53. * @param callBack

  54. * @throws Exception

  55. */

  56. public static void doPostAsyn(final String urlStr, final String params,

  57. final CallBack callBack) throws Exception

  58. {

  59. new Thread()

  60. {

  61. public void run()

  62. {

  63. try

  64. {

  65. String result = doPost(urlStr, params);

  66. if (callBack != null)

  67. {

  68. callBack.onRequestComplete(result);

  69. }

  70. } catch (Exception e)

  71. {

  72. e.printStackTrace();

  73. }

  74. };

  75. }.start();

  76. }

  77. /**

  78. * Get请求,获得返回数据

  79. *

  80. * @param urlStr

  81. * @return

  82. * @throws Exception

  83. */

  84. public static String doGet(String urlStr)

  85. {

  86. URL url = null;

  87. HttpURLConnection conn = null;

  88. InputStream is = null;

  89. ByteArrayOutputStream baos = null;

  90. try

  91. {

  92. url = new URL(urlStr);

  93. conn = (HttpURLConnection) url.openConnection();

  94. conn.setReadTimeout(TIMEOUT_IN_MILLIONS);

  95. conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);

  96. conn.setRequestMethod(“GET”);

  97. conn.setRequestProperty(“accept”, “*/*”);

  98. conn.setRequestProperty(“connection”, “Keep-Alive”);

  99. if (conn.getResponseCode() == 200)

  100. {

  101. is = conn.getInputStream();

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

如果你需要这些资料, ⬅ 专栏获取
   callBack.onRequestComplete(result);

  1. }

  2. } catch (Exception e)

  3. {

  4. e.printStackTrace();

  5. }

  6. };

  7. }.start();

  8. }

  9. /**

  10. * Get请求,获得返回数据

  11. *

  12. * @param urlStr

  13. * @return

  14. * @throws Exception

  15. */

  16. public static String doGet(String urlStr)

  17. {

  18. URL url = null;

  19. HttpURLConnection conn = null;

  20. InputStream is = null;

  21. ByteArrayOutputStream baos = null;

  22. try

  23. {

  24. url = new URL(urlStr);

  25. conn = (HttpURLConnection) url.openConnection();

  26. conn.setReadTimeout(TIMEOUT_IN_MILLIONS);

  27. conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);

  28. conn.setRequestMethod(“GET”);

  29. conn.setRequestProperty(“accept”, “*/*”);

  30. conn.setRequestProperty(“connection”, “Keep-Alive”);

  31. if (conn.getResponseCode() == 200)

  32. {

  33. is = conn.getInputStream();

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

[外链图片转存中…(img-ZxQRH8Jg-1719176641217)]

[外链图片转存中…(img-tCXbaSWI-1719176641217)]

[外链图片转存中…(img-zI8Wbb2z-1719176641218)]

[外链图片转存中…(img-5Fw8E3fF-1719176641218)]

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

如果你需要这些资料, ⬅ 专栏获取

  • 28
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值