我为什么会收到此错误:java.lang.IllegalArgumentException:此消费者需要类型为org.apache.http.HttpRequest的请求
CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer (CONSUMER_KEY,CONSUMER_SECRET);
consumer.setTokenWithSecret(oaut_token, tokenSecret);
URL url = new URL(targetURL);
request = (HttpURLConnection) url.openConnection();
// sign the request
consumer.sign(request);
// send the request
request.connect();
Run Code Online (Sandbox Code Playgroud)
编辑: 只是更新已接受的答案,因为它不再相关.由于HttpURLConnection上的错误,路标文档有点过时并建议在Android中使用CommonsHttpOAuthConsumer.这些已经修复,现在Android删除了Apache HTTP,因此现在通过DefaultOAuthConsumer处理路标的正确方法.
DefaultOAuthConsumer consumer = new DefaultOAuthConsumer (CONSUMER_KEY,CONSUMER_SECRET);
consumer.setTokenWithSecret(oaut_token, tokenSecret);
URL url = new URL(targetURL);
request = (HttpURLConnection) url.openConnection();
// sign the request
consumer.sign(request);
Run Code Online (Sandbox Code Playgroud)
在您发布的代码中应该很明显,这request
不是类型的HttpRequest
...
request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
Run Code Online (Sandbox Code Playgroud)
路标在android,lol上使用很简单,一旦你超过了那些不是最新,完整或者特别有用的教程.
无论如何这里有一种方法来使用apache http而不是本机android,它为了简洁起见有点难看,但应该让你运行起来.
修改你的代码以使其工作,你可能想让HttpClient在调用之间保持一致但我只是内联了所有这些.我还注意到你正在对令牌进行反序列化,所以我只是假设你有实际的OAuth流程.
祝好运!
CommonsHttpOAuthConsumer consumer = null;
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
consumer.setTokenWithSecret(oaut_token, tokenSecret);
// Use the apache method instead - probably should make this part persistent until
// you are done issuing API calls
HttpParams parameters = new BasicHttpParams();
HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(parameters, false);
HttpConnectionParams.setTcpNoDelay(parameters, true);
HttpConnectionParams.setSocketBufferSize(parameters, 8192);
HttpClient httpClient = new DefaultHttpClient();
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager tsccm = new ThreadSafeClientConnManager(parameters, schReg);
httpClient = new DefaultHttpClient(tsccm, parameters);
HttpGet get = new HttpGet(targetURL);
// sign the request
consumer.sign(get);
// send the request & get the response (probably a json object, but whatever)
String response = httpClient.execute(get, new BasicResponseHandler());
// shutdown the connection manager - last bit of the apache code
httpClient.getConnectionManager().shutdown();
//Do whatever you want with the returned info
JSONObject jsonObject = new JSONObject(response);
Run Code Online (Sandbox Code Playgroud)
而已
归档时间: |
|
查看次数: |
1818 次 |
最近记录: |