作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 mashape unirest 的新手,我似乎不知道我做错了什么。
我使用 maven 使用 unirest 作为依赖项,如下所示:
<dependencies>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.3.27</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
</dependencies>
接下来,我尝试使用 unirest 通过 java 匿名回调对我的服务器进行异步调用。
import java.util.*;
import java.util.concurrent.Future;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.lang.*;
import java.io.*;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.async.Callback;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.http.options.Options;
public class CRDTClient {
private ArrayList<String> servers;
public AtomicInteger successCount;
public CRDTClient() {
servers = new ArrayList(3);
servers.add("http://localhost:3000");
servers.add("http://localhost:3001");
servers.add("http://localhost:3002");
}
public boolean put(long key, String value) {
final CountDownLatch countDownLatch = new CountDownLatch(servers.size());
successCount = new AtomicInteger();
for (final String serverUrl : servers) {
Future<HttpResponse<JsonNode>> future = Unirest.post(serverUrl + "/cache/{key}/{value}")
.header("accept", "application/json")
.routeParam("key", Long.toString(key))
.routeParam("value", value).asJson()
.asJsonAsync(new Callback<JsonNode>() {
public void failed(UnirestException e) {
System.out.println("The request has failed: " + serverUrl);
countDownLatch.countDown();
}
public void completed(HttpResponse<JsonNode> response) {
int code = response.getStatus();
if (code == 200) {
successCount.incrementAndGet();
}
countDownLatch.countDown();
}
public void cancelled() {
System.out.println("The request has been cancelled");
}
});
}
// Block the thread until all responses gotten
countDownLatch.await();
return (Math.round(successCount.floatValue() / servers.size()) == 1);
}
运行时出现的错误
mvn clean package -e
是
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project client: Compilation failure
[ERROR] /Users/jonguan/Documents/SJSU/cmpe273/cmpe273-lab4/client/src/main/java/edu/sjsu/cmpe/cache/client/CRDTClient.java:[40,20] error: cannot find symbol
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project client: Compilation failure
.../cmpe/cache/client/CRDTClient.java:[40,20] error: cannot find symbol
结果是 .asJsonAsync 上的行
我尝试导入各种类,但似乎不起作用。
最佳答案
我明白了。我真是个傻子。
在上一行,有一个额外的 .asJson
.routeParam("value", value).asJson()
应该是
.routeParam("value", value)
关于java - mashape unirest java Future HttpResponse asJsonAsync 找不到符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27466955/
我是 mashape unirest 的新手,我似乎不知道我做错了什么。 我使用 maven 使用 unirest 作为依赖项,如下所示: com.mashape.unir
我是一名优秀的程序员,十分优秀!