2018-12-12 05:51:45 -05:00
|
|
|
package com.topjohnwu.net;
|
|
|
|
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.BufferedOutputStream;
|
2019-03-24 06:20:57 -04:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.Closeable;
|
2018-12-12 05:51:45 -05:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.FilterInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.HttpURLConnection;
|
2019-04-03 01:01:18 -04:00
|
|
|
import java.util.Scanner;
|
2018-12-12 05:51:45 -05:00
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
|
2019-03-24 06:20:57 -04:00
|
|
|
public class Request implements Closeable {
|
2018-12-12 05:51:45 -05:00
|
|
|
private HttpURLConnection conn;
|
|
|
|
private Executor executor = null;
|
|
|
|
private DownloadProgressListener progress = null;
|
2019-01-01 18:45:48 +08:00
|
|
|
private int code = -1;
|
|
|
|
|
|
|
|
ErrorHandler err = null;
|
2018-12-12 05:51:45 -05:00
|
|
|
|
|
|
|
private interface Requestor<T> {
|
|
|
|
T request() throws Exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Result<T> {
|
|
|
|
T result;
|
|
|
|
|
|
|
|
public T getResult() {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getCode() {
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2019-03-24 06:20:57 -04:00
|
|
|
public boolean isSuccess() {
|
|
|
|
return code >= 200 && code <= 299;
|
|
|
|
}
|
|
|
|
|
2018-12-12 05:51:45 -05:00
|
|
|
public HttpURLConnection getConnection() {
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Request(HttpURLConnection c) {
|
|
|
|
conn = c;
|
|
|
|
}
|
|
|
|
|
2019-03-24 06:20:57 -04:00
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
conn.disconnect();
|
|
|
|
}
|
|
|
|
|
2018-12-12 05:51:45 -05:00
|
|
|
public Request addHeaders(String key, String value) {
|
|
|
|
conn.setRequestProperty(key, value);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Request setDownloadProgressListener(DownloadProgressListener listener) {
|
|
|
|
progress = listener;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Request setErrorHandler(ErrorHandler handler) {
|
|
|
|
err = handler;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Request setExecutor(Executor e) {
|
|
|
|
executor = e;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-03-24 06:20:57 -04:00
|
|
|
public Result<Void> connect() {
|
|
|
|
try {
|
|
|
|
connect0();
|
|
|
|
} catch (IOException e) {
|
|
|
|
if (err != null)
|
|
|
|
err.onError(conn, e);
|
|
|
|
}
|
|
|
|
return new Result<>();
|
|
|
|
}
|
|
|
|
|
2018-12-12 05:51:45 -05:00
|
|
|
public Result<InputStream> execForInputStream() {
|
|
|
|
return exec(this::getInputStream);
|
|
|
|
}
|
|
|
|
|
2019-01-01 18:45:48 +08:00
|
|
|
public void getAsFile(File out, ResponseListener<File> rs) {
|
2018-12-12 05:51:45 -05:00
|
|
|
submit(() -> dlFile(out), rs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void execForFile(File out) {
|
|
|
|
exec(() -> dlFile(out));
|
|
|
|
}
|
|
|
|
|
2019-03-24 06:20:57 -04:00
|
|
|
public void getAsBytes(ResponseListener<byte[]> rs) {
|
|
|
|
submit(this::dlBytes, rs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<byte[]> execForBytes() {
|
|
|
|
return exec(this::dlBytes);
|
|
|
|
}
|
|
|
|
|
2018-12-12 05:51:45 -05:00
|
|
|
public void getAsString(ResponseListener<String> rs) {
|
|
|
|
submit(this::dlString, rs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<String> execForString() {
|
|
|
|
return exec(this::dlString);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getAsJSONObject(ResponseListener<JSONObject> rs) {
|
|
|
|
submit(this::dlJSONObject, rs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<JSONObject> execForJSONObject() {
|
|
|
|
return exec(this::dlJSONObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getAsJSONArray(ResponseListener<JSONArray> rs) {
|
|
|
|
submit(this::dlJSONArray, rs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<JSONArray> execForJSONArray() {
|
|
|
|
return exec(this::dlJSONArray);
|
|
|
|
}
|
|
|
|
|
2019-03-24 06:20:57 -04:00
|
|
|
private void connect0() throws IOException {
|
|
|
|
conn.connect();
|
|
|
|
code = conn.getResponseCode();
|
|
|
|
}
|
|
|
|
|
2018-12-12 05:51:45 -05:00
|
|
|
private <T> Result<T> exec(Requestor<T> req) {
|
|
|
|
Result<T> res = new Result<>();
|
|
|
|
try {
|
|
|
|
res.result = req.request();
|
2019-01-01 18:45:48 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
if (err != null)
|
|
|
|
err.onError(conn, e);
|
|
|
|
}
|
2018-12-12 05:51:45 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private <T> void submit(Requestor<T> req, ResponseListener<T> rs) {
|
|
|
|
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
|
|
|
|
try {
|
|
|
|
T t = req.request();
|
|
|
|
Runnable cb = () -> rs.onResponse(t);
|
|
|
|
if (executor == null)
|
|
|
|
Networking.mainHandler.post(cb);
|
|
|
|
else
|
|
|
|
executor.execute(cb);
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (err != null)
|
|
|
|
err.onError(conn, e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private BufferedInputStream getInputStream() throws IOException {
|
2019-03-24 06:20:57 -04:00
|
|
|
connect0();
|
2018-12-12 05:51:45 -05:00
|
|
|
InputStream in = conn.getInputStream();
|
|
|
|
if (progress != null) {
|
2019-01-01 18:45:48 +08:00
|
|
|
in = new ProgressInputStream(in, conn.getContentLength(), progress) {
|
2018-12-12 05:51:45 -05:00
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
super.close();
|
|
|
|
conn.disconnect();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
in = new FilterInputStream(in) {
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
super.close();
|
|
|
|
conn.disconnect();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return new BufferedInputStream(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String dlString() throws IOException {
|
2019-04-03 01:01:18 -04:00
|
|
|
try (Scanner s = new Scanner(getInputStream(), "UTF-8")) {
|
|
|
|
s.useDelimiter("\\A");
|
|
|
|
return s.next();
|
2018-12-12 05:51:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private JSONObject dlJSONObject() throws IOException, JSONException {
|
|
|
|
return new JSONObject(dlString());
|
|
|
|
}
|
|
|
|
|
|
|
|
private JSONArray dlJSONArray() throws IOException, JSONException {
|
|
|
|
return new JSONArray(dlString());
|
|
|
|
}
|
|
|
|
|
|
|
|
private File dlFile(File f) throws IOException {
|
|
|
|
try (InputStream in = getInputStream();
|
|
|
|
OutputStream out = new BufferedOutputStream(new FileOutputStream(f))) {
|
|
|
|
int len;
|
2019-03-24 06:20:57 -04:00
|
|
|
byte[] buf = new byte[4096];
|
2018-12-12 05:51:45 -05:00
|
|
|
while ((len = in.read(buf)) != -1) {
|
|
|
|
out.write(buf, 0, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
2019-03-24 06:20:57 -04:00
|
|
|
|
|
|
|
private byte[] dlBytes() throws IOException {
|
|
|
|
int len = conn.getContentLength();
|
|
|
|
len = len > 0 ? len : 32;
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream(len);
|
|
|
|
try (InputStream in = getInputStream()) {
|
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
while ((len = in.read(buf)) != -1) {
|
|
|
|
out.write(buf, 0, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.toByteArray();
|
|
|
|
}
|
2018-12-12 05:51:45 -05:00
|
|
|
}
|