安装Unirest-Java以使用Mashape API

时间:2013-10-31 14:18:53

标签: maven mashape unirest

我创建了这个简单的类:

import javax.swing.JOptionPane;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

public class TweetSent {

    public static void main(String[] args) {
        try {
            HttpResponse<JsonNode> request = Unirest.get("https://intridea-tweetsentiments.p.mashape.com/twitsentiment/?num_tweets=10&query=%3Cquery%3E").basicAuth("X-Mashape-Authorization", "#######################").asJson();
            JOptionPane.showMessageDialog(null,"request: "+request);
        } catch (UnirestException e) {
            JOptionPane.showMessageDialog(null,"ERROR: "+e);
            e.printStackTrace();
        }
    }

}

但是我收到了这个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64
    at com.mashape.unirest.request.HttpRequest.basicAuth(HttpRequest.java:61)
    at com.NR.V.TweetSent.main(TweetSent.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64

修改

我删除了<repository>文件中提到的所有pom.xml元素:

<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.3.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpasyncclient</artifactId>
    <version>4.0-beta4</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.3</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>

我是从Maven那里得到的:

[WARNING] The POM for com.mashape.unirest:unirest-java:jar:1.3.0-SNAPSHOT is missing, no dependency information available

enter image description here

2 个答案:

答案 0 :(得分:2)

我是Unirest-Java的作者。

你能否将Unirest更新到最新版本?应该已使用版本1.3.0修复此问题。

还要确保已导入所有必需的依赖项。您可以阅读https://github.com/Mashape/unirest-java#installing

上的安装说明

修改:要强制Maven重新下载库,请尝试删除unirest-java处的~/.m2/repository/com/mashape/unirest/unirest-java/文件夹并再次执行mvn clean compile

答案 1 :(得分:1)

建议使用Maven将所有东西打包在一个超级罐中。

请参阅此post,了解如何在Java项目中使用Unirest。