繁体   English   中英

在gradle项目中使用HttpUrlBuilder

[英]Use HttpUrlBuilder in gradle project

我有以下功能来构建网址。

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

我该如何解决?

您需要将依赖项添加到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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM