我正在尝试通过 Bitfinex 提供的公共 API 端点获取历史 Bitfinex 蜡烛数据。尝试在 kotlin 中使用 OkHttp 但仍然得到空结果。我猜这是编码问题,但无法弄清楚。网址=https://api.bitfinex.com/v2/candles/trade:1h:tBTCUSD/hist?sort=1。如果您将网址输入浏览器,您将获得数据。但使用下面的代码我得到 200 响应代码但空响应。请帮忙。代码:
fun getBitfinexCandleHist():String{
val client = OkHttpClient()
val url = HttpUrl.Builder()
.scheme("https")
.host("api.bitfinex.com")
.addPathSegment("v2")
.addPathSegment("candles")
.addPathSegment("trade:1h:tBTCUSD")
.addPathSegment("hist")
.addQueryParameter("sort","1")
.build()
println(url)
val request: Request = Request.Builder()
.url(url)
.build()
val call: Call = client.newCall(request)
val response: Response = call.execute()
return response.body().toString()
}
我认为这是OkHttp的问题。我用的是Unirest,没有问题。代码如下:
val url = "https://api-b.bitfinex.com/v2/candles/trade%3A1m%3AtBTCUSD/hist"
var urlparam = "?sort=+1"
urlparam = "$urlparam&limit=10000"
setDefaultHeaders()
val response: HttpResponse<*> = Unirest.get(url + urlparam)
.asJson()