From 345cd1795f18c665db483e8d87206821c2a5f744 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 1 Aug 2017 23:08:34 +0800 Subject: [PATCH] Update WebService --- .../topjohnwu/magisk/asyncs/CheckUpdates.java | 2 +- .../topjohnwu/magisk/asyncs/UpdateRepos.java | 4 +- .../com/topjohnwu/magisk/module/Repo.java | 2 +- .../topjohnwu/magisk/utils/WebService.java | 68 ++++++++----------- build.gradle | 2 +- 5 files changed, 32 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java index a7d4fb00f..a1aeeb3f9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java @@ -29,7 +29,7 @@ public class CheckUpdates extends ParallelTask { protected Void doInBackground(Void... voids) { MagiskManager magiskManager = getMagiskManager(); if (magiskManager == null) return null; - String jsonStr = WebService.request(UPDATE_JSON, WebService.GET); + String jsonStr = WebService.getString(UPDATE_JSON); try { JSONObject json = new JSONObject(jsonStr); JSONObject magisk = json.getJSONObject("magisk"); diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java index 951431e96..58723080d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java @@ -96,13 +96,13 @@ public class UpdateRepos extends ParallelTask { if (url == null) { url = String.format(Locale.US, REPO_URL, page + 1); } - String jsonString = WebService.request(url, WebService.GET, header, true); + String jsonString = WebService.getString(url, header); if (TextUtils.isEmpty(jsonString)) { // At least check the pages we know return page + 1 < etags.size() && loadPage(page + 1, null, CHECK_ETAG); } - // The request succeed, parse the new stuffs + // The getString succeed, parse the new stuffs try { loadJSON(jsonString); } catch (Exception e) { diff --git a/app/src/main/java/com/topjohnwu/magisk/module/Repo.java b/app/src/main/java/com/topjohnwu/magisk/module/Repo.java index 23187193d..b7f6bdcbc 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/Repo.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/Repo.java @@ -29,7 +29,7 @@ public class Repo extends BaseModule { } public void update() throws CacheModException { - String props = WebService.request(getManifestUrl(), WebService.GET); + String props = WebService.getString(getManifestUrl()); String lines[] = props.split("\\n"); parseProps(lines); Logger.dev("Repo: Fetching prop: " + getId()); diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java b/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java index 20a4dd110..84605a87f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java @@ -1,6 +1,8 @@ package com.topjohnwu.magisk.utils; import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; @@ -14,36 +16,31 @@ public class WebService { public final static int GET = 1; public final static int POST = 2; - /** - * Making web service call - * - * @url - url to make request - * @requestmethod - http request method - */ - public static String request(String url, int method) { - return request(url, method, null, true); + public static String getString(String url) { + return getString(url, null); } - public static String request(String url, int method, boolean newline) { - return request(url, method, null, newline); - } - - /** - * Making service call - * - * @url - url to make request - * @requestmethod - http request method - * @params - http request params - * @header - http request header - * @newline - true to append a newline each line - */ - public static String request(String urlAddress, int method, - Map header, boolean newline) { - Logger.dev("WebService: Service call " + urlAddress); - URL url; - StringBuilder response = new StringBuilder(); + public static String getString(String url, Map header) { + InputStream in = request(GET, url, header); + if (in == null) return ""; + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + String line; + StringBuilder builder = new StringBuilder(); try { - url = new URL(urlAddress); + while ((line = br.readLine()) != null) { + builder.append(line).append("\n"); + } + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + return builder.toString(); + } + + public static InputStream request(int method, String address, Map header) { + Logger.dev("WebService: Service call " + address); + try { + URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(15000); @@ -62,18 +59,7 @@ public class WebService { } } - int responseCode = conn.getResponseCode(); - - if (responseCode == HttpsURLConnection.HTTP_OK) { - String line; - BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); - while ((line = br.readLine()) != null) { - if (newline) { - response.append(line).append("\n"); - } else { - response.append(line); - } - } + if (conn.getResponseCode() == HttpsURLConnection.HTTP_OK) { if (header != null) { header.clear(); for (Map.Entry> entry : conn.getHeaderFields().entrySet()) { @@ -81,12 +67,12 @@ public class WebService { header.put(entry.getKey(), l.get(l.size() - 1)); } } + return conn.getInputStream(); } } catch (Exception e) { e.printStackTrace(); } - - return response.toString(); + return null; } } \ No newline at end of file diff --git a/build.gradle b/build.gradle index 4d230b80c..43fe7ffe2 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { maven { url "https://maven.google.com" } } dependencies { - classpath 'com.android.tools.build:gradle:3.0.0-alpha8' + classpath 'com.android.tools.build:gradle:3.0.0-alpha9' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files