mashape unirest java Future HttpResponse asJsonAsync не может найти символ

Я новичок в 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

is

[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

Я пытался импортировать все виды классов, но, похоже, это не работает.


person Jon    schedule 14.12.2014    source источник


Ответы (1)


Я понял. Глупый я.

В предыдущей строке есть лишний .asJson

.routeParam("value", value).asJson()

должно быть просто

.routeParam("value", value)
person Jon    schedule 15.12.2014