首页 > 代码库 > 保存一下简单封装的工具类JsonUtils 、android网络判断的Utils 是否连接无wifi、sdcard状态的utils

保存一下简单封装的工具类JsonUtils 、android网络判断的Utils 是否连接无wifi、sdcard状态的utils

1.JsonUtils    json和实体类之间相互转换      随便提一下  App版本升级   github上有开源框架可以看一下VersionUpdate;

public class JsonUtils {
/**
* 将一个实体类转换成json字符串(对象中可以包含集合)
*/
public static <T> String beanToJson(T t){
Gson gson = new Gson();
String json = gson.toJson(t);
return json;
}

/**
* 将一个json字符串,转换成一个实体类对象(可包含集合)
*/
public static <T> T jsonToBean(String json,Class<T> tClass) throws IllegalAccessException, InstantiationException {
Gson gson = new Gson();
T t = tClass.newInstance();
t = gson.fromJson(json, tClass);
return t;
}

/**
* json字符串转换成Json对象
*/
public static JSONObject stringToJson(String json){
try {
return new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}

/**
* 将输入流转换成String字符串
*/
public static String getString(InputStream in){
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = in.read(buffer)) != -1){
byteArrayOutputStream.write(buffer,0,len);
}
byte[] bytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
return new String(bytes,"utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

/**
* assret文件夹中读取json文件,然后转换为json对象
*/
public static JSONObject getJsonDataFromAssert(Context context,String jsonFileName) throws IOException, JSONException {
JSONObject jsonObject = null;
StringBuffer sb = new StringBuffer();
InputStream inputStream = context.getAssets().open(jsonFileName);
int len = -1;
byte[] buffer = new byte[1024];
while ((len = inputStream.read(buffer)) != -1){
sb.append(new String(buffer,0,len,"utf-8"));
}
inputStream.close();
jsonObject = new JSONObject(sb.toString());
return jsonObject;
}
}


2.android网络判断的Utils   是否连接无wifi

public class NetworkUtils {
//没有网络
public static final int NO_NETWORK = 0;
//wifi状态
public static final int WIFI = 1;
//没有wifi状态
public static final int NO_WIFI = 2;


/**
* 检测当前是否有网络连接
*/
public static boolean checkNetWork(Context context){
ConnectivityManager connectManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectManager == null){
return false;
}
NetworkInfo networkInfo = connectManager.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isAvailable()){
return false;
}
return true;
}

3.sdcard状态的utils
public class SdCardState {
/**
* 判断sd卡显示是什么状态,不存在,移除,没挂载,正在检测,挂载(正常状态)等;
*@return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
* {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
* {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
* {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
* {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
*/
public static String getSdStatus(){
return Environment.getExternalStorageState();
}

/**
* 判断SD卡是否是挂载状态,是否可用
*/
public static boolean sdAvailable(){
return Environment.MEDIA_MOUNTED.equals(getSdStatus());
}

/**
* 得到sd卡根目录的路径
*/
public static File getRootDirectory(){
return sdAvailable() ? Environment.getExternalStorageDirectory() : null;
}
}





/**
* 判断当前的网络类型是否是WIFI
*/
public static int checkNetWorkTypeForWifi(Context context){
if (!checkNetWork(context)){
return NO_NETWORK;
}
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()){
return WIFI;
}
return NO_WIFI;
}
}

 

保存一下简单封装的工具类JsonUtils 、android网络判断的Utils 是否连接无wifi、sdcard状态的utils