我正试图通过在Kotlin使用雅虎财务api获取股票价格信息。https://financequotes-api.com/ --我编写了引用描述的代码,但是我一直把StringIndexOutOfBoundsException: String索引超出了范围:-1,出现了一个错误。等级和最优惠的设置附在下面。
package com.jym.assetallocation
class MainActivity : AppCompatActivity() {
private val vBinding by lazy {ActivityMainBinding.inflate(layoutInflater)}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(vBinding.root)
vBinding.testBtn.setOnClickListener {
Thread(Runnable {
try{
val stock = YahooFinance.get("AAPL")
val price = stock.quote.price
Log.d("test", price.toString())
}catch(err:Exception){
Log.d("test", err.toString())
}
this@MainActivity.runOnUiThread(java.lang.Runnable {
})
}).start()
}
}
}dependencies {
implementation group: 'com.yahoofinance-api', name: 'YahooFinanceAPI', version: '3.5.0'
implementation 'org.jsoup:jsoup:1.15.3'
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

2022-10-04 14:29:16.291 29579-29605/com.jym.assetallocation W/System.err: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
2022-10-04 14:29:16.291 29579-29605/com.jym.assetallocation W/System.err: at java.lang.String.substring(String.java:2064)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation W/System.err: at yahoofinance.quotes.stock.StockQuotesRequest.parseCSVLine(StockQuotesRequest.java:126)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation W/System.err: at yahoofinance.quotes.stock.StockQuotesRequest.parseCSVLine(StockQuotesRequest.java:11)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation W/System.err: at yahoofinance.quotes.QuotesRequest.getResult(QuotesRequest.java:95)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation W/System.err: at yahoofinance.YahooFinance.getQuotes(YahooFinance.java:355)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation W/System.err: at yahoofinance.YahooFinance.get(YahooFinance.java:85)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation W/System.err: at yahoofinance.YahooFinance.get(YahooFinance.java:69)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation W/System.err: at com.jym.assetallocation.MainActivity.onCreate$lambda-2$lambda-1(MainActivity.kt:25)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation W/System.err: at com.jym.assetallocation.MainActivity.$r8$lambda$3MbZcd1DL5t6rNGYiArXMujwWms(Unknown Source:0)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation W/System.err: at com.jym.assetallocation.MainActivity$$ExternalSyntheticLambda1.run(Unknown Source:2)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation W/System.err: at java.lang.Thread.run(Thread.java:923)
2022-10-04 14:29:16.292 29579-29605/com.jym.assetallocation D/test: kotlin.Unit发布于 2022-10-04 14:06:12
由于我直接下载了文件的最新版本并使用它,所以它正常工作。

plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.jym.yftest"
minSdk 22
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
buildFeatures{
viewBinding true
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}class MainActivity : AppCompatActivity() {
private val vBinding by lazy { ActivityMainBinding.inflate(layoutInflater)}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(vBinding.root)
vBinding.testBtn.setOnClickListener {
Thread(Runnable {
try{
val stock = YahooFinance.get("AAPL")
val price = stock.quote.price
Log.d("test", price.toString())
val stock2 = YahooFinance.get("VOO")
val price2 = stock2.quote.price
Log.d("test", price2.toString())
}catch(err:Exception){
Log.d("test", err.toString())
}
this@MainActivity.runOnUiThread(java.lang.Runnable {
})
}).start()
}
}
}结果
2022-10-04 23:00:40.799 2328-2377/com.jym.yftest D/test: 145.14 2022-10-04 23:00:40.952 2328-2377/com.jym.yftest D/test: 346.076
https://stackoverflow.com/questions/73921459
复制相似问题