android - "None of the following functions can be called with the arguments supplied"带燃料 HTTP

标签 android http kotlin

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

  1. “以下函数都不能用提供的参数调用。” - 关于对 responseString 的引用 Screenshot

  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),您还需要为后续参数使用默认值,否则您将需要使用 named arguments :

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

因为:

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

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

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

关于android - "None of the following functions can be called with the arguments supplied"带燃料 HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38367915/

相关文章:

android - 无法使用 Android 版本 4.0.3 创建模拟器

android - 在应用程序处于后台时更改语言

windows - 在 Perl 中发送 HTTP 请求

java - 如何防止servlet在收到get请求时创建新 session ?

java - 非通用子类继承(实现)通用父类(super class)(接口(interface))

android - 单击通知图标时如何将应用程序置于前面(来自服务)?

android - ViewModelScope 被永久取消了?

json - Swift:将 JSON 从 URL 转换为多个字符串

kotlin - 如何正确加入 CoroutineScope 中启动的所有作业

android - 如何在不使用 Gradle 复制 jar 的情况下将一个模块的源包含在另一个模块中?