【问题标题】:"None of the following functions can be called with the arguments supplied" with Fuel HTTP“使用提供的参数不能调用以下函数”Fuel HTTP
【发布时间】:2016-07-14 07:19:46
【问题描述】:

我在 Android Studio 中创建了一个新的 Kotlin 项目,现在我正在尝试在该项目中使用 Fuel HTTP library。但是,当我尝试使用 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()
}

如何解决这些错误?

【问题讨论】:

    标签: android http kotlin


    【解决方案1】:

    responseString 有几个重载,您在 Readme 中看到经常使用的重载具有以下签名:

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

    如您所见,第一个参数有一个默认值。另请注意,第二个(也是最后一个)参数是一个没有默认值的 lambda。如果您选择使用默认参数值 (charset),您还需要为后续参数使用默认值,否则您将需要使用 named arguments

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

    因为:

    在 Kotlin 中,有一个约定,如果 function 是一个函数,该参数可以在 括号

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

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

    【讨论】:

    • 在我自己解决问题之前,我尝试了您列出的最后一种方法,但没有奏效。我现在通过更新 Android Studio 解决了这个问题。不过感谢您的回复。
    • @OrangeFlash81 自行更新 Android Studio 并不能解决您在问题中发布的编译问题。
    • 我更新了 Android Studio,然后导致 Kotlin 插件更新。我根本没有更改我的代码。
    • @OrangeFlash81 好吧,Fuel.get("http://httpbin.org/get").responseString({request, response, result -&gt; }) 在我的机器上的 Kotlin 编译器的 1.0.3 版本中无法编译 - 原因在答案中已解释。 注意括号 - 没有它们它会编译。
    • 哦,你是对的。对于那个很抱歉。然而,在更新之前,不管有没有括号,它都不起作用。
    【解决方案2】:

    知道了 - 我使用的是非常旧的 Android Studio 版本(1.1,最新的是 2.1!)。更新 Android Studio 修复了我提到的所有错误,以及我在修补缺少某些内置 SDK 功能时遇到的其他一些错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多