知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
使用字元串拼接 URL地址特別容易出錯
String url = "https://www.netkiller.cn/article?username="+ username + "&category="+ category;
較好的處理方式是使用 HttpUrl.Builder
HttpUrl.Builder builder = HttpUrl.parse("https://www.netkiller.cn/article").newBuilder(); builder.addQueryParameter("username", "netkiller"); builder.addQueryParameter("category", "android"); String url = builder.build().toString(); Log.d("okhttp", url);
輸出結果
https://www.netkiller.cn/article?username=netkiller&category=android