|
|
@@ -7,7 +7,9 @@ import android.graphics.Bitmap;
|
|
|
import android.graphics.BitmapFactory;
|
|
|
import android.net.ConnectivityManager;
|
|
|
import android.net.NetworkInfo;
|
|
|
+import android.net.Uri;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.Environment;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
|
|
|
@@ -46,6 +48,7 @@ import java.io.BufferedWriter;
|
|
|
import java.io.DataOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.InputStreamReader;
|
|
|
@@ -66,6 +69,8 @@ import java.security.KeyStoreException;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.security.UnrecoverableKeyException;
|
|
|
import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
@@ -267,10 +272,10 @@ public class Network
|
|
|
|
|
|
parms.putString("app_ver", RedAntApplication.getInstance()
|
|
|
.getPackageManager()
|
|
|
- .getPackageInfo("com.usai.redant.raimage", 0).versionName);
|
|
|
+ .getPackageInfo("com.usai.redant.redantmobile", 0).versionName);
|
|
|
parms.putInt("app_short_ver", RedAntApplication.getInstance()
|
|
|
.getPackageManager()
|
|
|
- .getPackageInfo("com.usai.redant.raimage", 0).versionCode);
|
|
|
+ .getPackageInfo("com.usai.redant.redantmobile", 0).versionCode);
|
|
|
|
|
|
parms.putString("deviceid",RedAntApplication.device_id);
|
|
|
|
|
|
@@ -1822,50 +1827,142 @@ public class Network
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static String download_query(Bundle params) {
|
|
|
+ public static String downloadFile(Bundle params, String download_url) {
|
|
|
+
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ String fileName = UUID.randomUUID().toString();
|
|
|
+ String dir = "download";
|
|
|
+ if (RAUtil.isFileExist(fileName,dir)) {
|
|
|
+ return dir + File.separator + fileName;
|
|
|
+ } else {
|
|
|
+ // 创建一个URL对象
|
|
|
+ URL url=new URL(download_url);
|
|
|
+ // 创建一个HTTP链接
|
|
|
+ connection=(HttpURLConnection)url.openConnection();
|
|
|
+
|
|
|
+ // 拼参数
|
|
|
+ if (params != null && params.keySet().size() > 0) {
|
|
|
+ OutputStream os = connection.getOutputStream();
|
|
|
+ BufferedWriter writer = new BufferedWriter(
|
|
|
+ new OutputStreamWriter(os, "UTF-8"));
|
|
|
+ writer.write(createPostParameters(params));
|
|
|
+
|
|
|
+ writer.flush();
|
|
|
+ writer.close();
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取响应
|
|
|
+ JSONObject responseHeader = getResponseHeader(connection);
|
|
|
+ String suggestedFileName = getResponsesuggestedFilename(responseHeader);
|
|
|
+ Log.d("Download", "download_query: " + suggestedFileName);
|
|
|
+ if (suggestedFileName != null && !suggestedFileName.isEmpty()) {
|
|
|
+ fileName = suggestedFileName;
|
|
|
+ }
|
|
|
|
|
|
- if(BuildConfig.DEBUG && true){
|
|
|
- //do something for assert aim
|
|
|
- Log.e("Result", "onClick: not implement");
|
|
|
+ // 使用IO流获取数据
|
|
|
+ InputStream inputStream=connection.getInputStream();
|
|
|
+ // 写文件
|
|
|
+ File downloadFile = RAUtil.write2SDFromInput(fileName,dir,inputStream);
|
|
|
+
|
|
|
+ if (downloadFile == null) {
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ return downloadFile.getAbsolutePath();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (connection != null) {
|
|
|
+ connection.disconnect();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-// HttpURLConnection connection = null;
|
|
|
-// try {
|
|
|
-//
|
|
|
-// URL url = new URL("https://www.baidu.com/");
|
|
|
-// connection = (HttpURLConnection)url.openConnection();
|
|
|
-// connection.setRequestMethod("GET");
|
|
|
-// connection.setUseCaches(false);
|
|
|
-// connection.setConnectTimeout(1500);
|
|
|
-// connection.setReadTimeout(1500);
|
|
|
-//
|
|
|
-// BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
-// StringBuilder response = new StringBuilder();
|
|
|
-// String line;
|
|
|
-// while ((line = reader.readLine()) != null) {
|
|
|
-// response.append(line);
|
|
|
-// }
|
|
|
-//
|
|
|
-// } catch (IOException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// } finally {
|
|
|
-// if (connection != null) {
|
|
|
-// connection.disconnect();
|
|
|
-// }
|
|
|
-// }
|
|
|
|
|
|
|
|
|
-// //创建下载任务,downloadUrl就是下载链接
|
|
|
-// DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl));
|
|
|
-// //指定下载路径和下载文件名
|
|
|
-// request.setDestinationInExternalPublicDir("/download/", fileName);
|
|
|
-// //获取下载管理器
|
|
|
-// DownloadManager downloadManager= (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
-// //将下载任务加入下载队列,否则不会进行下载
|
|
|
-// downloadManager.enqueue(request);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String download_query(String download_url) {
|
|
|
+
|
|
|
+ return downloadFile(null,download_url);
|
|
|
+ }
|
|
|
+
|
|
|
+ //读取响应头
|
|
|
+ public static JSONObject getResponseHeader(HttpURLConnection conn) {
|
|
|
+ Map<String, List<String>> responseHeaderMap = conn.getHeaderFields();
|
|
|
+ int size = responseHeaderMap.size();
|
|
|
+ try {
|
|
|
+ JSONObject responseHeader = new JSONObject();
|
|
|
+ for(int i = 0; i < size; i++){
|
|
|
+ String responseHeaderKey = conn.getHeaderFieldKey(i);
|
|
|
+ String responseHeaderValue = conn.getHeaderField(i);
|
|
|
+ if (responseHeaderKey != null && !responseHeaderKey.isEmpty() && responseHeaderValue != null) {
|
|
|
+ responseHeader.put(responseHeaderKey,responseHeaderValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return responseHeader;
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getResponsesuggestedFilename(JSONObject response) {
|
|
|
+
|
|
|
+ if (response == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ String key = "Content-Disposition";
|
|
|
+ String content_disposition = response.getString(key);
|
|
|
+ if (content_disposition != null) {
|
|
|
+ String[] components = content_disposition.split(";");
|
|
|
+ if (components[1] != null) {
|
|
|
+ String fileName = components[1].split("=")[1];
|
|
|
+ fileName = fileName.replace("\"","");
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
+ public static JSONObject kv_detail(Bundle params) {
|
|
|
+
|
|
|
+ params.putString("_action","ant_mobile");
|
|
|
+ params.putString("module","kv_detail");
|
|
|
+ params.putString("_operate","row_action");
|
|
|
+
|
|
|
+ String json_string = getJson(RedAntApplication.server,params);
|
|
|
+
|
|
|
+ if (json_string == null || json_string.isEmpty()) {
|
|
|
+ json_string = "{\"result\" : \"1\"}";
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return new JSONObject(json_string);
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static JSONObject require_orderdetail(String url,Bundle param) {
|
|
|
+
|
|
|
+ return request_editor(url,param);
|
|
|
+ }
|
|
|
+
|
|
|
}
|