“使用Fuel HTTP提供的参数不能调用以下任何函数”

我在Android Studio中创建了一个新的Kotlin项目,现在我正在尝试在该项目中使用Fuel HTTP库 。 但是,当我尝试使用GitHub自述文件中的示例中的函数时,出现两个错误:

  1. “下面的函数都不能用提供的参数来调用。” – 对responseString的引用 截图

  2. “无法推断此参数的类型,请明确指定。” – 对回调函数的每个参数

这是我正在使用的代码:

package me.myname.morefueltesting import android.support.v7.app.ActionBarActivity import android.os.Bundle import android.view.Menu import android.view.MenuItem import com.github.kittinunf.fuel.Fuel public class MainActivity : ActionBarActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) /* First error /--Second errors--\ | | | | \/\/\/\/\/\/\/ \/\/\/\ /\/\/\/\ /\/\/\ */ Fuel.get("http://httpbin.org/get").responseString({request, response, result -> // Some callback code here }) } } 

我的build.gradle

 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { compileSdkVersion 22 buildToolsVersion '22.0.1' defaultConfig { applicationId "me.myname.morefueltesting" minSdkVersion 15 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main.java.srcDirs += 'src/main/kotlin' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.0' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile 'com.github.kittinunf.fuel:fuel-rxjava:1.3.1' compile 'com.github.kittinunf.fuel:fuel-android:1.3.1' } buildscript { ext.kotlin_version = '1.0.3' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } repositories { mavenCentral() } 

我怎样才能解决这些错误?

responseString有几个重载,在Readme中使用了很多,它有以下签名:

 fun responseString( charset: Charset = Charsets.UTF_8, handler: (Request, Response, Result<String, FuelError>) -> Unit): Request 

正如你所看到的第一个参数有一个默认值。 还要注意第二个(也是最后一个)参数是一个没有默认值的lambda。 如果您选择使用默认参数值( charset ),则还需要为后续参数使用默认值,否则您将需要使用命名参数 :

 Fuel.get("http://httpbin.org/get").responseString(handler = {request, response, result -> // Some callback code her }) 

因为:

在Kotlin中有一个约定,如果一个函数的最后一个参数是一个函数,那么该参数可以在圆括号之外指定

您也可以使用自述文件中指定的方法,但不使用括号

 Fuel.get("http://httpbin.org/get").responseString {request, response, result -> // Some callback code her } 

明白了 – 我正在使用Android Studio的一个非常旧的版本(最新版本是2.1时! 更新Android Studio修复了我提到的所有错误,以及我在修补缺少一些内置SDK函数时遇到的其他错误。