WHy am I get this error : java.lang.IllegalArgumentException: This consumer expects requests of type 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();
EDIT: Just updating the accepted answer as it is not relevant anymore. the signpost documentation is a bit outdated and suggest to use CommonsHttpOAuthConsumer in Android due to bugs on HttpURLConnection. These have been fixed and now Android removed the Apache HTTP so the correct way to deal with signpost is now via 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);
lang. IllegalArgumentException: No matching constant . Due to this exception the application is failing , this looks like a bug in the Spring code . Since the http status code received is not in the list spring framework is looking forit failed .
Because IllegalArgumentException is an unchecked exception, the Java compiler doesn't force you to catch it. Neither do you need to declare this exception in your method declaration's throws clause.
Some common unchecked exceptions in Java are NullPointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException.
Signpost is trivial to use on android, lol, once you get past the tutorials that are not really up to date, or complete, or in particularly useful order.
Anyway here is one way to do this using apache http instead of native android, it's a bit ugly for the sake of brevity but should get you up and running.
Modifed your code a bit to make it work, you probably want to make the HttpClient consistent across calls but I just inlined all that. I also notice you are deserializing the tokens so I am just going to assume that you have the actual OAuth flow working.
Good luck!
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);
That's it
It should be obvious in the code you posted that request
is not of type HttpRequest
...
request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With