HTTP http = HTTP.builder()
.baseUrl("http://api.demo.com") // 設(shè)置 BaseUrl
.build();
該配置全局生效,在配置了BaseUrl
之后,具體的請(qǐng)求便可以省略BaseUrl
部分,使得代碼更加簡潔,例如:
http.sync("/users").get() // http://api.demo.com/users
http.sync("/auth/signin") // http://api.demo.com/auth/signin
.addBodyParam("username", "Jackson")
.addBodyParam("password", "xxxxxx")
.post() // POST請(qǐng)求
在配置了BaseUrl
之后,如有特殊請(qǐng)求任務(wù),仍然可以使用全路徑的方式,一點(diǎn)都不妨礙:
http.sync("https://www.baidu.com").get()
如果覺得 OkHttps 好用,就去點(diǎn)個(gè) Star 吧,項(xiàng)目地址:
更多建議: