在gradle项目中使用HttpUrlBuilder

时间:2018-05-04 07:24:26

标签: java gradle build.gradle

我有以下功能来构建Url。

public String buildURL(Departments department) {
        URL url = new HttpUrl.Builder()
                .scheme("http")
                .host("localhost")
                .port(Integer.valueOf(department.getPort()))
                .addPathSegments(department.getName()+"/synchronization")
                .build().url();
        return url.toString();
    }

在build.gradle中,我使用依赖项。

compile group: 'com.squareup.okio', name: 'okio', version: '1.0.0'
compile 'com.squareup.okhttp3:okhttp:3.10.0

当我尝试调用上面的函数时,我收到以下错误。

Servlet.service() for servlet [dispatcherServlet] in context with path [/central] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: okio/Buffer] with root cause

java.lang.ClassNotFoundException: okio.Buffer

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您需要将依赖项添加到buildscript部分,以便它们可用于构建本身,而不是正在编译的模块:

buildscript {
    repositories.jcenter()
    dependencies.add("classpath", "com.squareup.okio:okio:1.0.0")
    dependencies.add("classpath", "com.squareup.okhttp3:okhttp:3.10.0")
}

URL url = new okhttp3.HttpUrl.Builder()
    .scheme("http")
    .host("localhost")
    .port(3333)
    .addPathSegments("address")
    .build().url()