2013-10-31 89 views
0

我創建了這個簡單的類:安裝Unirest Java的消耗Mashape API

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 

編輯:

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

<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

我的Unirest-Java作者。

您能不能更新Unirest到其最新版本?這個問題應該已被修復版本1.3.0

此外請確保已導入所有必需的依賴關係。您可以在https://github.com/Mashape/unirest-java#installing

編輯閱讀安裝說明:要強制Maven來重新下載庫,嘗試~/.m2/repository/com/mashape/unirest/unirest-java/刪除unirest-java文件夾,然後再執行mvn clean compile

+0

Maven說它找不到工件1.3.0-SNAPSHOT,但它可以找到1.2.8 – ManInMoon

+0

Unirest現在使用Maven Central。嘗試刪除條目。正如你所看到的,它已經正確上傳:https://oss.sonatype.org/content/repositories/snapshots/com/mashape/unirest/unirest-java/1.3.0-SNAPSHOT/ – Mark

+0

仍然沒有運氣。請參閱有關EDIT 2的問題。 – ManInMoon

1

建議使用Maven將所有內容封裝在一個超級罐子裏。

關於如何在您的Java項目中使用Unirest,請參閱此post