OAuth的与Android [英] OAuth with Android
问题描述
我试图访问Yelp的API。我已经得到我的钥匙,看了大约40篇解释什么,我需要做的,我已经尝试了一切。关键的,哪些不是有效。我收到以下内容:
05-16 17:39:54.955:E / AndroidRuntime(538):java.lang.NoClassDefFoundError的:oauth.signpost.commonshttp.CommonsHttpOAuthConsumer
我已经导入了commons- codeC-1.6罐,路标,commonshttp4-1.2.1.2.jar和路标 - 核心1.2.1.2.jar。
我仍然得到错误信息。任何帮助吗?
进口java.net.HttpURLConnection中;
进口的java.net.URL;
进口的java.util.HashMap;
进口的java.util.Map;进口oauth.signpost.OAuth;
进口oauth.signpost.OAuthConsumer;
进口oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
进口oauth.signpost.exception.OAuthCommunicationException;
进口oauth.signpost.exception.OAuthExpectationFailedException;
进口oauth.signpost.exception.OAuthMessageSignerException;
/ **
*例用于访问Yelp的API。
* /
公共类Yelp的{
私人字符串consumerKey =;
私人字符串consumerSecret =;
私人字符串标记=;
私人字符串tokenSecret =; / **
*搜索与字词和位置。
*
* @参数项检索词
* @参数经纬度经纬度
* @参数经度经度
* @返回JSON字符串响应
* /
公共字符串搜索(串词,双纬度,经度双)抛出OAuthMessageSignerException,OAuthExpectationFailedException,OAuthCommunicationException { 尝试{
OAuthConsumer myConsumer =新CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
myConsumer.setTokenWithSecret(令牌,tokenSecret); 字符串urlToPost =http://api.yelp.com/v2/search;
地图<字符串,字符串> MYMAP =新的HashMap<字符串,字符串>();
myMap.put(OAuth.OAUTH_CONSUMER_KEY,consumerKey);
myMap.put(OAuth.OAUTH_TOKEN,令牌);
myMap.put(OAuth.OAUTH_SIGNATURE_METHODHMAC-SHA1);
myMap.put(OAuth.OAUTH_SIGNATURE,tokenSecret);
myMap.put(OAuth.OAUTH_TIMESTAMP,Long.toString(System.currentTimeMillis的()/ 1000L));
myMap.put(OAuth.OAUTH_NONCEASDF); urlToPost = OAuth.addQueryParameters(urlToPost,MYMAP); HttpURLConnection的myCon =(HttpURLConnection类)(新URL(urlToPost))的openConnection()。
myConsumer.sign(myCon); myCon.connect();
返回myCon.getResponseMessage();
}赶上(例外五){
e.printStackTrace();
}
返回null;
}
}
转到项目 - >属性,并根据您的Java构建路径 - >订单和出口标签确保这些JAR文件检查
和,只是以供将来参考,如果这些是你真正的钥匙,我会确保为不可以把它们放在公共网站上。只是在说。
I am attempting to access the Yelp API. I have obtained my keys and have read about 40 articles explaining what I need to do and I have attempted everything. The key and what not are valid. I am getting the following:
05-16 17:39:54.955: E/AndroidRuntime(538): java.lang.NoClassDefFoundError: oauth.signpost.commonshttp.CommonsHttpOAuthConsumer
I have imported the commons-codec-1.6 jar, the signpost-commonshttp4-1.2.1.2.jar, and the signpost-core-1.2.1.2.jar.
I am still getting the error message. Any help?
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;
}
}
Go to Project -> Properties, and under your Java Build Path -> Order and Export tab make sure those JARs are checked.
And, just for future reference, if those are your real keys, I would make sure to not put them on a public website. Just sayin.
这篇关于OAuth的与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!