Fabii Fabii - 2 months ago 15
Java Question

Why am I getting this error java.lang.IllegalArgumentException?

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);

Answer

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