OKHttp 基本使用紀錄

OKHttp 是支援HTTP & HTTP/2 的開源框架,透過它可以很方便發送HTTP請求。 具有以下特性: 1.API設計簡單,可以透過幾行code即可發送HTTP請求 2.支援同步,異步請求。同步請求會block目前的Thread,異步請求則不會block OKHttp官網連結 最新版本釋出 以下紀錄最基本的get, post, post with json format 的使用方式。 1.相依性 在 module 的 build.gradle 加入 dependencies { ... implementation 'com.squareup.okhttp3:okhttp:3.11.0' } Note:目前(2018/09/28)最新版本為3.11.0 2. Using in code 為了封裝OKHttp建立了OKHttpWrapper 類別,外部皆透過該類別來使用OKHttp。 OKHttpWrapper.java import android.util.Log; import java.io.IOException; import java.util.Map; import java.util.Set; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody;...

Arrays.binarySearch 直接使用於 android resource 的問題

為了方便使用陣列常常會直接在array.xml中宣告2個互相對應的陣列 e.g., <string-array name="type"> <item>0</item> <item>1</item> <item>2</item> <item>3</item> <item>4</item> <item>5</item> </string-array> <string-array name="code"> <item>A</item> <item>C</item> <item>B</item> <item>E</item> <item>F</item> <item>G</item> <item>D</item> </string-array> 若直接在code中使用Arrays.binsearch 會出現 index 錯誤 e.g., String[] targetArray = getResources().getStringArray(R.array.type); int index = Arrays.binarySearch(targetArray,...

Google Cloud Source Repositories 如何將本地端已存在的repo推送到遠端

狀況:本地端已存在git repo,現在想將本地端的repo推送到遠端的新repo 1.先到GCSR新增遠端新repo 到https://source.cloud.google.com/repos ->點擊新增存放區->點擊建立新的存放區->點擊繼續->輸入存放區名稱->選擇已存在專案或新增專案->點擊建立 2.建立本地端repo的認證並推送到遠端 在"請選取將程式碼推送至存放區的選項:"選擇"從本機 Git 存放區推送程式碼" ->在"選取您偏好的驗證方式:"選擇"手動產生的憑證" ->點擊 1.產生及儲存Git憑證->將Configure Git 頁面中的 Windows Users 程式碼複製並貼到本地端的git bash -> 移動到本地端專案路徑並進行 2. 進行將 Cloud Repository 新增為遠端存放區:(這裡注意最好編輯成1行再貼到git bash,如 git remote add google https://xxxx)->最後進行 3.從本機 Git 存放區推送程式碼: (注意若出現 fatal: remote error: Invalid authentication credentials. Please generate a new identifier: https://source.developers.google.com/new-password 代表要重新產生憑證,到https://source.developers.google.com/auth/start?scopes=https://www.googleapis.com/auth/cloud-platform...

Google Cloud Source Repositories 試用紀錄

GCSR(Google Cloud Source Repositories) 是 google 推出以 git 為基礎的程式碼代管服務。 原先我的side project是自己建git server來放置source code,後來因為硬體故障等等 自己維護需要花費不少時間和成本,加上本身也是google 的愛好者,最後選擇GCSR來試試。 首先談談GCSR的缺點,基本上最大麻煩的點為google 提供的repository必須依附於google cloud platform(GCP)上的專案。 也就是說你必須先了解基本的GCP使用方式才能開始用GCSR。 第2個麻煩的點在於create, clone repository在官方教學中必須透過 google cloud sdk才能使用(也許可以透過別的方式但還未試出),也就是說你必須先安裝 google cloud sdk才能開始使用GCSR。 第3個麻煩的點在於免費版的限制,如下 免費方案 超量 最多 5 位使用者 每位專案使用者每月 $1 美元 (專案使用者達 5 位以上時) 50 GB 儲存空間 每 GB 儲存空間 每月 $0.10 美元 50 GB 輸出 每 GB 輸出每月 $0.10 美元 若以上3個限制您都還可以接受,那就繼續往下看吧。 首先提供官網GCSR的說明。若想直接參考使用方式請看quick start。 1.在Google Cloud...

使用 RingtoneManager 播放提示音(Notification),鬧鐘(Alarm),來電鈴聲(Ringtone)

1.播放預設的提示音。 public static void playDefaultNotification(Context context) { Uri defaultNotificationURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone soundOfDefaultNotification = RingtoneManager.getRingtone(context, defaultNotificationURI); soundOfDefaultNotification.play(); } 其中RingtoneManager.TYPE_NOTIFICATION為提示音的設定。 若想播放預設的鬧鐘只要改為RingtoneManager.TYPE_ALARM 播放預設的鬧鐘只要改為RingtoneManager.TYPE_RINGTONE 2.播放裝置內其他的提示音。 首先取得裝置內的提示音清單。 public static Map<String, String> getSoundTitleAndUri(Context context, int type) { RingtoneManager manager = new RingtoneManager(context); manager.setType(type); Cursor cursor = manager.getCursor(); Map<String, String> titleAndUri = new HashMap<>(); while (cursor.moveToNext()) { ...

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews