教學來源:
Kotlin Youtube - How to Quickly Fetch Parse JSON with OkHttp and Gson (Ep 2)
GSON Tutorial Part 1 - SIMPLE (DE)SERIALIZATION - Android Studio Tutorial
Simple HTTP Request with OkHttp - Android Studio Tutorial
整理:
1
AlertDialog.Builder dynamically setMessage
不能:
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
if(true){
result = "登入成功!";
builder.setMessage(result);
}
else{
result = "登入失敗!";
builder.setMessage(result);
}
AlertDialog alertDialog = builder.create();
alertDialog.show();
可以:
if(true){
result = "登入成功!";
new AlertDialog.Builder(Activity.this).setMessage(result).create().show();
}
else{
result = "登入失敗!";
new AlertDialog.Builder(Activity.this).setMessage(result).create().show();
}
2
如果有這個錯誤:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
要用:
Activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
String result = "登入成功,您好!";
new AlertDialog.Builder(BarcodeActivity.this).setMessage(result).create().show();
}
});
3
OkHttp可以看到http request、response之類的
稍微了解一下,參考:
HTTP協議頭部與Keep-Alive模式詳解
4
How to set connection timeout with OkHttp
你以为的timeout,不一定是用户的timeout
maxRequestsPerHost
How do I await Retrofit async web service call? (call.enqueue)
Retrofit 2 – Synchronous and asynchronous call example
Retrofit 2 起手式
Logging with Retrofit 2
Retrofit + MVVM + RecyclerView + CallBack | All in one APP
Retrofit Tutorial Part 2 - URL MANIPULATION: @PATH @QUERY @QUERYMAP @URL - Android Studio Tutorial
Retrofit 操作教學
Gson教程七(译):@SerializedName和@Expose
Android Image Loading from a String URL
Intercept and retry call by means of OkHttp Interceptors
Android-Retrofit-超时-重试-缓存-拦截器
Android Room with a View - Java
Model View View-Model (MVVM): Getting Started
#3 RecyclerView with MVVM - ViewModel and Fragment
HttpsURLConnection可能會在ReadTimeout時自動重新嘗試
Java :HttpUrlConnection發送GET取得Json
Android Paging Library Tutorial using Retrofit
Performance optimisations for android applications - Part 4 Fixing concurrency
Android app memory 基本分析
Android中的OutOfMemoryError
彻底解析Android缓存机制——LruCache
Android Bitmap最全面详解
Android: Create Your Own Image Loading Library in Kotlin #DIY
WeakHashMap 的使用
深入理解 java 中的 Soft references & Weak references & Phantom reference
整理:
1
在android寫這一段
final List<byte[]> container = new ArrayList<>();
byte[] b = new byte[1000 * 1000 * 1000];
container.add(b);
超過記憶體了(OOM),所以會有這個錯誤:
java.lang.OutOfMemoryError: Failed to allocate a 1000000012 byte allocation with 2132250 free bytes and 380MB until OOM
2
通常自己清掉東西,就是類似 a= null 。
WeakHashMap 就是hashmap裡的Entry<K,V> extends WeakReference ,所以可以 過一段時間 , 沒人用的時候,
自動清掉。
可以寫個迴圈 比較 WeakHashMap 和 hashmap。不過寫迴圈好像不一定會被清掉。
可能要用 System.gc(); 測試 ,參考:
Hashmap vs WeakHashMap in Java
Java筆記 — final, finally 與 finalize
3
WeakReference 也是類似,可以寫個迴圈,看是不是過一段時間,變成null了。