Update WebService

This commit is contained in:
topjohnwu 2017-08-01 23:08:34 +08:00
parent 959aaee045
commit 345cd1795f
5 changed files with 32 additions and 46 deletions

View File

@ -29,7 +29,7 @@ public class CheckUpdates extends ParallelTask<Void, Void, Void> {
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
MagiskManager magiskManager = getMagiskManager(); MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return null; if (magiskManager == null) return null;
String jsonStr = WebService.request(UPDATE_JSON, WebService.GET); String jsonStr = WebService.getString(UPDATE_JSON);
try { try {
JSONObject json = new JSONObject(jsonStr); JSONObject json = new JSONObject(jsonStr);
JSONObject magisk = json.getJSONObject("magisk"); JSONObject magisk = json.getJSONObject("magisk");

View File

@ -96,13 +96,13 @@ public class UpdateRepos extends ParallelTask<Void, Void, Void> {
if (url == null) { if (url == null) {
url = String.format(Locale.US, REPO_URL, page + 1); 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)) { if (TextUtils.isEmpty(jsonString)) {
// At least check the pages we know // At least check the pages we know
return page + 1 < etags.size() && loadPage(page + 1, null, CHECK_ETAG); 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 { try {
loadJSON(jsonString); loadJSON(jsonString);
} catch (Exception e) { } catch (Exception e) {

View File

@ -29,7 +29,7 @@ public class Repo extends BaseModule {
} }
public void update() throws CacheModException { public void update() throws CacheModException {
String props = WebService.request(getManifestUrl(), WebService.GET); String props = WebService.getString(getManifestUrl());
String lines[] = props.split("\\n"); String lines[] = props.split("\\n");
parseProps(lines); parseProps(lines);
Logger.dev("Repo: Fetching prop: " + getId()); Logger.dev("Repo: Fetching prop: " + getId());

View File

@ -1,6 +1,8 @@
package com.topjohnwu.magisk.utils; package com.topjohnwu.magisk.utils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
@ -14,36 +16,31 @@ public class WebService {
public final static int GET = 1; public final static int GET = 1;
public final static int POST = 2; public final static int POST = 2;
/** public static String getString(String url) {
* Making web service call return getString(url, null);
*
* @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 request(String url, int method, boolean newline) { public static String getString(String url, Map<String, String> header) {
return request(url, method, null, newline); InputStream in = request(GET, url, header);
} if (in == null) return "";
BufferedReader br = new BufferedReader(new InputStreamReader(in));
/** String line;
* Making service call StringBuilder builder = new StringBuilder();
*
* @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<String, String> header, boolean newline) {
Logger.dev("WebService: Service call " + urlAddress);
URL url;
StringBuilder response = new StringBuilder();
try { 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<String, String> header) {
Logger.dev("WebService: Service call " + address);
try {
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000); conn.setReadTimeout(15000);
@ -62,18 +59,7 @@ public class WebService {
} }
} }
int responseCode = conn.getResponseCode(); if (conn.getResponseCode() == HttpsURLConnection.HTTP_OK) {
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 (header != null) { if (header != null) {
header.clear(); header.clear();
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) { for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
@ -81,12 +67,12 @@ public class WebService {
header.put(entry.getKey(), l.get(l.size() - 1)); header.put(entry.getKey(), l.get(l.size() - 1));
} }
} }
return conn.getInputStream();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return null;
return response.toString();
} }
} }

View File

@ -7,7 +7,7 @@ buildscript {
maven { url "https://maven.google.com" } maven { url "https://maven.google.com" }
} }
dependencies { 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 // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files