提交 0791ebc8 编写于 作者: B Blankj

See 02/11 log

上级 f563ed8c
......@@ -5,7 +5,10 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.View
import com.blankj.utilcode.util.*
import com.blankj.utilcode.util.AntiShakeUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.Utils
import com.r0adkll.slidr.Slidr
/**
......@@ -60,6 +63,5 @@ abstract class BaseActivity : AppCompatActivity(), IBaseView {
override fun onDestroy() {
super.onDestroy()
AppUtils.unregisterAppStatusChangedListener(this)
KeyboardUtils.fixSoftInputLeaks(this);
}
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ import java.util.Scanner;
/**
* Create by MilkZS on 2019/1/9 13:36
*/
public class HttpsUtil {
public final class HttpsUtil {
private static final int CONNECT_TIMEOUT_TIME = 15000;
private static final int READ_TIMEOUT_TIME = 19000;
......
package com.blankj.utilcode.util;
import android.accounts.NetworkErrorException;
import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* <pre>
* author: blankj
* blog : http://blankj.com
* time : 2019/02/08
* desc : utils about http
* </pre>
*/
public class HttpUtils {
private static final int CONNECT_TIMEOUT_TIME = 15000;
private static final int READ_TIMEOUT_TIME = 19000;
private static final TrustManager[] DEFAULT_TRUST_MANAGERS = new TrustManager[]{
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) { /**/ }
@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) { /**/ }
}
};
private static final HostnameVerifier DEFAULT_VERIFIER = new HostnameVerifier() {
@SuppressLint("BadHostnameVerifier")
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
private static final Config CONFIG = new Config();
private HttpUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
public static void doGet(@NonNull final Request request, final BaseResponse response) {
if (response == null) return;
HttpURLConnection conn = null;
try {
conn = getConnection(request, "GET");
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
InputStream is = conn.getInputStream();
response.onSuccess(is);
is.close();
} else if (responseCode == 301 || responseCode == 302) {
String location = conn.getHeaderField("Location");
doGet(request, response);
} else {
response.onError(new NetworkErrorException("Response code: " + responseCode));
}
} catch (IOException e) {
response.onError(e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
public static void doPost(@NonNull final Request request,
final BaseResponse response) {
if (response == null) return;
HttpURLConnection conn = null;
try {
conn = getConnection(request, "POST");
addHeader(conn, request.header);
// conn.setRequestProperty("accept", "*/*");
// conn.setRequestProperty("connection", "Keep-Alive");
// conn.setRequestProperty("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
// conn.setRequestProperty("content-length", String.valueOf(sb.length()));
// conn.setDoOutput(true);
//
// PrintWriter printWriter = new PrintWriter(conn.getOutputStream());
// printWriter.write(sb.toString());
// printWriter.flush();
// printWriter.close();
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
InputStream is = conn.getInputStream();
response.onSuccess(is);
is.close();
} else {
response.onError(new NetworkErrorException("Response code: " + responseCode));
}
} catch (IOException e) {
response.onError(e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private static HttpURLConnection getConnection(final Request request, final String requestMethod) throws IOException {
URL httpUrl = new URL(request.url);
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
if (conn instanceof HttpsURLConnection) {
HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
trustAllHosts(httpsConn);
}
conn.setConnectTimeout(CONFIG.connectTimeout);
conn.setReadTimeout(CONFIG.readTimeout);
conn.setUseCaches(CONFIG.useCaches);
conn.setRequestMethod(requestMethod);
return conn;
}
private static void trustAllHosts(HttpsURLConnection conn) {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, DEFAULT_TRUST_MANAGERS, new java.security.SecureRandom());
conn.setSSLSocketFactory(sslContext.getSocketFactory());
conn.setHostnameVerifier(DEFAULT_VERIFIER);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void addHeader(final HttpURLConnection conn, final Map<String, String> headerMap) {
if (headerMap != null) {
for (String key : headerMap.keySet()) {
conn.setRequestProperty(key, headerMap.get(key));
}
}
}
private static boolean isSpace(final String s) {
if (s == null) return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
public static class Config {
private int connectTimeout = CONNECT_TIMEOUT_TIME;
private int readTimeout = READ_TIMEOUT_TIME;
private boolean useCaches = false;
}
public static class Request {
private String url;
private Map<String, String> header;
private RequestBody body;
public static Request withUrl(@NonNull final String url) {
return new Request(url);
}
private Request(final String url) {
this.url = url;
}
public Request addHeader(@NonNull final String name, @NonNull final String value) {
if (header == null) {
header = new HashMap<>();
}
header.put(name, value);
return this;
}
public Request addHeader(@NonNull final Map<String, String> header) {
if (this.header == null) {
this.header = new HashMap<>();
}
this.header.putAll(header);
return this;
}
public Request post(@NonNull final RequestBody body) {
this.body = body;
return this;
}
}
public static final class RequestBody {
String mediaType;
byte[] content;
long length;
private RequestBody(String mediaType, byte[] content) {
this.mediaType = mediaType;
this.content = content;
length = content == null ? 0 : content.length;
}
private static String getCharsetFromMediaType(String mediaType) {
mediaType = mediaType.toLowerCase().replace(" ", "");
int index = mediaType.indexOf("charset=");
if (index == -1) return "utf-8";
int st = index + 8;
int end = mediaType.length();
for (int i = st; i < end; i++) {
char c = mediaType.charAt(i);
if ((c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '-') {
end = i;
break;
}
}
if (st < end) {
String charset = mediaType.substring(st, end);
if (Charset.isSupported(charset)) return charset;
}
throw new IllegalArgumentException("MediaType is not correct: \"" + mediaType + "\"");
}
public static RequestBody create(String mediaType, byte[] content) {
return new RequestBody(mediaType, content);
}
public static RequestBody form(final Map<String, String> form) {
return form(form, "utf-8");
}
public static RequestBody form(final Map<String, String> form, String charset) {
String mediaType = "application/x-www-form-urlencoded;charset=" + charset;
if (form != null) {
final StringBuilder sb = new StringBuilder();
for (String key : form.keySet()) {
if (sb.length() != 0) {
sb.append("&");
}
sb.append(key).append("=").append(form.get(key));
}
try {
return new RequestBody(mediaType, sb.toString().getBytes(charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return new RequestBody(mediaType, null);
}
public static RequestBody json(final String json) {
return json(json, "utf-8");
}
public static RequestBody json(final String json, String charset) {
String mediaType = "application/json;charset=" + charset;
if (json != null) {
try {
return new RequestBody(mediaType, json.getBytes(charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return new RequestBody(mediaType, null);
}
public static RequestBody file(String mediaType, final File file) {
return file(mediaType, file);
}
}
public static abstract class BaseResponse {
public abstract void onSuccess(InputStream is);
public abstract void onError(Throwable t);
}
}
......@@ -267,8 +267,8 @@ public final class KeyboardUtils {
(InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null) return;
String[] leakViews = new String[]{"mLastSrvView", "mCurRootView", "mServedView", "mNextServedView"};
for (String leakView : leakViews) {
try {
try {
for (String leakView : leakViews) {
Field leakViewField = InputMethodManager.class.getDeclaredField(leakView);
if (leakViewField == null) continue;
if (!leakViewField.isAccessible()) {
......@@ -280,10 +280,8 @@ public final class KeyboardUtils {
if (view.getRootView() == activity.getWindow().getDecorView().getRootView()) {
leakViewField.set(imm, null);
}
} catch (Throwable th) {
th.printStackTrace();
}
}
} catch (Throwable ignore) { /**/ }
}
/**
......
......@@ -42,7 +42,7 @@ public final class RomUtils {
private static final String[] ROM_HTC = {"htc"};
private static final String[] ROM_SONY = {"sony"};
private static final String[] ROM_GIONEE = {"gionee", "amigo"};
private static final String[] ROM_MOTOROLA = {"motorola"};
private static final String[] ROM_MOTOROLA = {"motorola"};
private static final String VERSION_PROPERTY_HUAWEI = "ro.build.version.emui";
private static final String VERSION_PROPERTY_VIVO = "ro.vivo.os.build.display.id";
......@@ -449,8 +449,8 @@ public final class RomUtils {
@Override
public String toString() {
return "RomInfo{name: " + name +
"\nversion: " + version + "}";
return "RomInfo{name=" + name +
", version=" + version + "}";
}
}
}
\ No newline at end of file
......@@ -341,8 +341,8 @@ public final class Utils {
(InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null) return;
String[] leakViews = new String[]{"mLastSrvView", "mCurRootView", "mServedView", "mNextServedView"};
for (String leakView : leakViews) {
try {
try {
for (String leakView : leakViews) {
Field leakViewField = InputMethodManager.class.getDeclaredField(leakView);
if (leakViewField == null) continue;
if (!leakViewField.isAccessible()) {
......@@ -354,10 +354,8 @@ public final class Utils {
if (view.getRootView() == activity.getWindow().getDecorView().getRootView()) {
leakViewField.set(imm, null);
}
} catch (Throwable th) {
th.printStackTrace();
}
}
} catch (Throwable ignore) { /**/ }
}
}
......
......@@ -29,8 +29,6 @@ public class BaseTest {
@Test
public void test() throws Exception {
// final CountDownLatch countDownLatch = new CountDownLatch(1);
// final Scanner scanner = new Scanner(System.in);
// ExecutorService singlePool = ThreadUtils.getSinglePool();
......
package com.blankj.utilcode.util;
import org.junit.Test;
/**
* <pre>
* author: blankj
* blog : http://blankj.com
* time : 2019/02/10
* desc :
* </pre>
*/
public class HttpUtilsTest extends BaseTest {
@Test
public void doGet() {
// HttpUtils.doGet("http://raw.githubusercontent.com/Blankj/AndroidUtilCode/85bc44d1c8adb31027870ea4cb7a931700c80cad/LICENSE", new HttpUtils.BaseResponse() {
// @Override
// public void onSuccess(InputStream is) {
// LogUtils.json(ConvertUtils.inputStream2String(is, "UTF-8"));
// }
//
// @Override
// public void onError(Throwable t) {
// LogUtils.e(t);
// }
// });
}
@Test
public void doPost() {
// HashMap<String, String> map = new HashMap<>();
// map.put("username", "blankj");
// map.put("password", "blankj");
// HttpUtils.doPost("http://www.wanandroid.com/user/login",
// map,
// new HttpUtils.BaseResponse() {
// @Override
// public void onSuccess(InputStream is) {
// LogUtils.json(ConvertUtils.inputStream2String(is, "UTF-8"));
// }
//
// @Override
// public void onError(Throwable t) {
// LogUtils.e(t);
// }
// });
// HttpUtils.doGet("http://www.wanandroid.com/lg/collect/list/0/json",
// new HttpUtils.BaseResponse() {
// @Override
// public void onSuccess(InputStream is) {
// LogUtils.json(ConvertUtils.inputStream2String(is, "UTF-8"));
// }
//
// @Override
// public void onError(Throwable t) {
// LogUtils.e(t);
// }
// });
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册