Aaron Christiansen
Aaron Christiansen

Reputation: 11837

"None of the following functions can be called with the arguments supplied" with Fuel HTTP

I've created a new Kotlin project in Android Studio, and I'm now trying to use the Fuel HTTP library in that project. However, when I try to use the functions like in the examples on the GitHub readme, I get two errors:

  1. "None of the following functions can be called with the arguments supplied." - on the reference to responseString Screenshot

  2. "Cannot infer a type for this parameter. Please specify it explicitly." - on each of the arguments to the callback function

This is the code I am using:

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
        })
    }
}

My 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()
}

How can I resolve these errors?

Upvotes: 3

Views: 6351

Answers (2)

miensol
miensol

Reputation: 41678

There are several overloads of responseString the one you see used a lot in Readme has following signature:

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

As you can see the first parameter has a default value. Notice also that the second (and at the same last) argument is a lambda that has no default value. If you choose to use a default parameter value (charset) you need to also use default values for subsequent parameters or you'll need to use named arguments:

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

Because:

In Kotlin, there is a convention that if the last parameter to a function is a function, that parameter can be specified outside of the parentheses

You can also use the method as specified in Readme but without parentheses:

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

Upvotes: 3

Aaron Christiansen
Aaron Christiansen

Reputation: 11837

Got it - I was using an extremely old version of Android Studio (1.1 when latest is 2.1!). Updating Android Studio fixed all of the errors I mentioned, as well as some others that I encountered while tinkering where some built-in SDK functions were missing.

Upvotes: 0

Related Questions