3

我正在尝试访问 Yelp API。我已经拿到了我的钥匙,并阅读了大约 40 篇解释我需要做什么的文章,并且我已经尝试了一切。密钥和无效的内容。我得到以下信息:

05-16 17:39:54.955: E/AndroidRuntime(538): java.lang.NoClassDefFoundError: oauth.signpost.commonshttp.CommonsHttpOAuthConsumer

我已经导入了 commons-codec-1.6 jar、signpost-commonshttp4-1.2.1.2.jar 和 signpost-core-1.2.1.2.jar。

我仍然收到错误消息。有什么帮助吗?

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;


/**
 * Example for accessing the Yelp API.
 */
public class Yelp {
  private String consumerKey = "";
  private String consumerSecret = "";
  private String token = "";
  private String tokenSecret = "";

  /**
   * Search with term and location.
   *
   * @param term Search term
   * @param latitude Latitude
   * @param longitude Longitude
   * @return JSON string response
   */
  public String search(String term, double latitude, double longitude) throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException {



    try {
        OAuthConsumer myConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
        myConsumer.setTokenWithSecret(token, tokenSecret);

        String urlToPost = "http://api.yelp.com/v2/search";
        Map<String, String> myMap = new HashMap<String, String>();
        myMap.put(OAuth.OAUTH_CONSUMER_KEY, consumerKey);
        myMap.put(OAuth.OAUTH_TOKEN, token);
        myMap.put(OAuth.OAUTH_SIGNATURE_METHOD, "HMAC-SHA1");
        myMap.put(OAuth.OAUTH_SIGNATURE, tokenSecret);
        myMap.put(OAuth.OAUTH_TIMESTAMP, Long.toString(System.currentTimeMillis() / 1000L));
        myMap.put(OAuth.OAUTH_NONCE, "asdf");

        urlToPost = OAuth.addQueryParameters(urlToPost, myMap);

        HttpURLConnection myCon = (HttpURLConnection) (new URL(urlToPost)).openConnection();
        myConsumer.sign(myCon);

        myCon.connect();
        return myCon.getResponseMessage();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
  }
}
4

2 回答 2

5

转到 Project -> Properties,然后在您的 Java Build Path -> Order and Export 选项卡下确保检查了这些 JAR。

而且,为了将来参考,如果这些是你真正的钥匙,我会确保不要把它们放在公共网站上。只是在说。

于 2012-05-16T17:48:38.253 回答
0

我在 Android 应用程序中也面临同样的问题。当我使用signpost -core-1.2.1.2 但它无法解析CommonsHttpOAuthConsumer。我从http://www.java2s.com/Code/Jar/s/Downloadsignpostcommonshttp41211jar.htm下载它

有效。

于 2014-12-23T04:25:18.940 回答