From 07673027f0374997b4e05e93e6d76aebbe0af284 Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 9 Aug 2023 20:39:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20TaskInfo.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/iku/os/net/TaskInfo.java | 101 +++++++++++++++++++++ app/src/main/java/iku/os/net/taskInfo.java | 86 ------------------ 2 files changed, 101 insertions(+), 86 deletions(-) create mode 100644 app/src/main/java/iku/os/net/TaskInfo.java delete mode 100644 app/src/main/java/iku/os/net/taskInfo.java diff --git a/app/src/main/java/iku/os/net/TaskInfo.java b/app/src/main/java/iku/os/net/TaskInfo.java new file mode 100644 index 0000000..e0df640 --- /dev/null +++ b/app/src/main/java/iku/os/net/TaskInfo.java @@ -0,0 +1,101 @@ +package iku.os.net; + +import android.os.AsyncTask; +import android.util.Log; + +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + + +public class TaskInfo { + private static final String TAG = "IkuClient"; + private static String savePath = "/sdcard/apks/"; + private static String getTaskUrl = "http://123.56.44.45/tt/ddj/autoApk.do"; + private static String downloadUrl = "http://39.103.73.250/tt/upload/ddj/"; + private static boolean completed = false; + + public static void getTask() { + new GetTaskAsyncTask().execute(); + } + + private static class GetTaskAsyncTask extends AsyncTask { + @Override + protected JSONObject doInBackground(Void... voids) { + HttpURLConnection conn = null; + JSONObject json = null; + try { + conn = (HttpURLConnection) new URL(getTaskUrl).openConnection(); + conn.setConnectTimeout(5000); + conn.setRequestMethod("GET"); + Log.i(TAG, "getTask ResponseCode: " + conn.getResponseCode()); + if (conn.getResponseCode() == 200) { + StringBuilder sb = new StringBuilder(); + try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } + } + json = new JSONObject(sb.toString()); + } + } catch (Exception e) { + Log.e(TAG, "taskInfo getTask: ", e); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + return json; + } + + @Override + protected void onPostExecute(JSONObject result) { + if (result != null) { + try { + int code = result.getInt("code"); + if (code == 1) { + String apkPath = result.getString("apkPath"); + String packageName = result.getString("packageName"); + Log.i(TAG, "getTask apkPath: " + apkPath); + Log.i(TAG, "getTask packageName: " + packageName); + // 拼接URL并启动下载 + new Thread(new Runnable() { + @Override + public void run() { + downloadFile(downloadUrl + apkPath, savePath + apkPath); + // 执行其他下载操作 + } + }).start(); + } + } catch (Exception e) { + Log.e(TAG, "Error parsing JSON: ", e); + } + } + } + } + + + public static void downloadFile(String urlStr, String savePath) { + try (InputStream input = new URL(urlStr).openStream(); + FileOutputStream output = new FileOutputStream(savePath)) { + + byte[] buffer = new byte[1024]; + int bytesRead; + + while ((bytesRead = input.read(buffer)) != -1) { + output.write(buffer, 0, bytesRead); + } + completed = true; + Log.i(TAG, "Download completed: " + savePath); + } catch (IOException e) { + Log.e(TAG, "Error downloading file: ", e); + } + } +} diff --git a/app/src/main/java/iku/os/net/taskInfo.java b/app/src/main/java/iku/os/net/taskInfo.java deleted file mode 100644 index 110d376..0000000 --- a/app/src/main/java/iku/os/net/taskInfo.java +++ /dev/null @@ -1,86 +0,0 @@ -package iku.os.net; - -import android.util.Log; - -import org.json.JSONObject; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; - - -public class taskInfo { - private static final String TAG = "IkuClient"; - private static boolean completed = false; - - - - - public static void getTask() { - String packageName = ""; - int code = -1; - // 检查目录是否存在 - File directory = new File("/sdcard/apks/"); - if (!directory.exists()) { - directory.mkdirs(); // 如果目录不存在,则创建它 - } - - try { - HttpURLConnection conn = (HttpURLConnection) new URL("http://123.56.44.45/tt/ddj/autoApk.do").openConnection(); - conn.setConnectTimeout(5000); - conn.setRequestMethod("GET"); - Log.i(TAG, "getTask ResponseCode: "+conn.getResponseCode()); - if (conn.getResponseCode() == 200) { - StringBuilder sb = new StringBuilder(); - try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { - String line; - while ((line = br.readLine()) != null) { - sb.append(line); - } - } - JSONObject json = new JSONObject(sb.toString()); - code = json.getInt("code"); - if (code == 1){ - String apkPath = json.getString("apkPath"); - packageName = json.getString("packageName"); - Log.i(TAG, "getTask apkPath: "+apkPath); - Log.i(TAG, "getTask packageName: "+packageName); - String downloadUrl = "http://39.103.73.250/tt/upload/ddj/" + apkPath; - // 拼接URL并启动下载 - new Thread(new Runnable() { - @Override - public void run() { - downloadFile(downloadUrl, "" + apkPath); - // 执行其他下载操作 - } - }).start(); - } - } - } catch (Exception e) { - Log.e(TAG, "taskInfo getTask: ", e); - } - } - - - public static void downloadFile(String urlStr, String savePath) { - try (InputStream input = new URL(urlStr).openStream(); - FileOutputStream output = new FileOutputStream(savePath)) { - - byte[] buffer = new byte[1024]; - int bytesRead; - - while ((bytesRead = input.read(buffer)) != -1) { - output.write(buffer, 0, bytesRead); - } - completed = true; - Log.i(TAG, "Download completed: " + savePath); - } catch (IOException e) { - Log.e(TAG, "Error downloading file: ", e); - } - } -}