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) {
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");

View File

@ -96,13 +96,13 @@ public class UpdateRepos extends ParallelTask<Void, Void, Void> {
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) {

View File

@ -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());

View File

@ -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<String, String> header, boolean newline) {
Logger.dev("WebService: Service call " + urlAddress);
URL url;
StringBuilder response = new StringBuilder();
public static String getString(String url, Map<String, String> 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<String, String> 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<String, List<String>> 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;
}
}

View File

@ -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