我在 pom.xml 文件中添加了我的 maven 依赖项。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bioID.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
我想向 BioID 发出请求以接收 Web Token API。应用程序标识符和秘密当然被省略了。
package com.bioID.app;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Credentials;
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
public static String returntoken() {
// using OkHttpClient from the OkHttp library
HttpUrl url = HttpUrl.parse("https://bws.bioid.com/extension/token").newBuilder()
.addQueryParameter("id", APP_IDENTIFIER)
.build();
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization", Credentials.basic(APP_IDENTIFIER, APP_SECERET))
.build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
String token = response.body().string();
if (response.code() == 200) {
System.out.println("token=" + token);
}
return token;
}
}
尽管从依赖项中添加了类并参考了文档,但这以错误 Request.Builder 无法解析为类型结束。这是由于使用了错误的 okio 或 okhttp 库版本。
编译时的错误信息
mvn clean install