Response的onFailure参数修改为HttpException

This commit is contained in:
huangzhuanghua
2017-01-11 12:00:20 +08:00
parent 93ec154b7e
commit a8a9e58fe9
42 changed files with 162 additions and 277 deletions

View File

@ -75,14 +75,6 @@ public class FileUtils {
public static String getPlatformPicDir(Context context) {
return checkDir(getDir(context, "PlatformPic"));
}
public static String getAdPicDir(Context context) {
return checkDir(getDir(context, "AdPic"));
}
public static String getPicPath(Context context, String name) {
return checkDir(getDir(context, "Pic")) + File.separator + name;
}
private static String checkDir(String dir) {
File directory = new File(dir);
@ -119,10 +111,7 @@ public class FileUtils {
public static boolean isEmptyFile(String path) {
File file = new File(path);
if (file.exists() && file.length() != 0) {
return false;
}
return true;
return !(file.exists() && file.length() != 0);
}
public static boolean isMounted() {
@ -141,9 +130,7 @@ public class FileUtils {
return context.getFilesDir().getAbsolutePath() + File.separator + dir;
}
/*
* 返回剩余空间 单位MB
*/
// 返回剩余空间 单位MB
@SuppressWarnings("deprecation")
public static float getFreeSpaceByPath(String path) {
StatFs statfs = new StatFs(path);
@ -173,8 +160,7 @@ public class FileUtils {
}
// 下载文件
public static int downloadFile(String url, String savePath)
throws IOException {
public static int downloadFile(String url, String savePath) {
DataInputStream dis = null;
FileOutputStream fos = null;
try {
@ -198,32 +184,36 @@ public class FileUtils {
fos.write(buffer, 0, len);
}
fos.flush();
fos.close();
dis.close();
}
return code;
} catch (IOException e) {
e.printStackTrace();
return -1;
} finally {
if (fos != null) {
fos.close();
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dis != null) {
dis.close();
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return -1;
}
// 上传文件
public static JSONObject uploadFile(String url, String filePath, String token) {
String end = "\r\n";
String twoHyphens = "--";
String boundary = UUID.randomUUID().toString().replaceAll("-", "")
.substring(0, 16);
String boundary = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url)
.openConnection();
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
/*
* Output to the connection. Default is false, set to true because
* post method must write something to the connection
@ -240,8 +230,7 @@ public class FileUtils {
// 设置请求属性
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
if (token != null) {
connection.setRequestProperty("TOKEN", token);
}
@ -255,13 +244,11 @@ public class FileUtils {
Utils.log("length = " + file.length());
}
// 设置DataOutputStreamgetOutputStream中默认调用connect()
DataOutputStream dos = new DataOutputStream(
connection.getOutputStream()); // output
DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); // output
// to the connection
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; "
+ "name=\"Filedata\";filename=\"" + file.getName() + "\""
+ end);
+ "name=\"Filedata\";filename=\"" + file.getName() + "\"" + end);
dos.writeBytes(end);
// 取得文件的FileInputStream
FileInputStream fStream = new FileInputStream(file);