5

为什么我会收到此错误: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();

编辑: 只需更新已接受的答案,因为它不再相关。路标文档有点过时,由于 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);
4

4 回答 4

5

在您发布的代码中应该很明显request不是类型HttpRequest...

request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
于 2012-04-13T17:37:36.837 回答
5

路标在 android 上使用起来很简单,哈哈,一旦你通过了那些不是真正最新的、不完整的或特别有用的顺序的教程。

无论如何,这是使用 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);

而已

于 2012-04-13T22:05:10.547 回答
1

当方法需要一个参数类型并且它接收到另一种类型时,抛出异常 java.lang.IllegalArgumentException。
在这种情况下,方法是sign,参数是request

consumer.sign(request); 

它正在等待接收HTTPRequest类型并且它正在接收另一种类型。

于 2012-04-13T17:54:09.967 回答
0
public class BlockTicketPostOauth extends AsyncTask<String, Void, Integer> {
    ProgressDialog pd;
    String response;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd=new ProgressDialog(BusPassengerInfo.this);
        pd.setMessage("wait continue to payment....");
        pd.show();

    }

    @Override
    protected Integer doInBackground(String... params) {
        InputStream inputStream = null;
        String ul ="http://api.seatseller.travel/blockTicket";

        String  JSONPayload=params[0];
        Integer result = 0;
        try {

            OAuthConsumer consumer = new CommonsHttpOAuthConsumer(YOUR CONSUMER KEY,YOUR CONSSUMER SECRETE); consumer.setTokenWithSecret(null, null);

            /* create Apache HttpClient */
            HttpClient httpclient = new DefaultHttpClient();

            /* Httppost Method */
            HttpPost httppost = new HttpPost(ul);

            // sign the request
            consumer.sign(httppost);

            // send json string to the server
            StringEntity paras =new StringEntity(JSONPayload);

            //seting the type of input data type
            paras.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

            httppost.setEntity(paras);

            HttpResponse httpResponse= httpclient.execute(httppost);

            int statusCode = httpResponse.getStatusLine().getStatusCode();

            Log.i("response json:","status code is:"+statusCode);
            Log.i("response json:","status message?:"+httpResponse.getStatusLine().toString());

            /* 200 represents HTTP OK */
            if (statusCode ==  200) {
                /* receive response as inputStream */
                inputStream = httpResponse.getEntity().getContent();
                response = convertInputStreamToString(inputStream);

                Log.i("response json:","json response?:"+response);

                Log.i("response block ticket :","status block key:"+response);

                result = 1; // Successful
            } else{
                result = 0; //"Failed to fetch data!";
            }
        } catch (Exception e) {
            Log.d("response error", e.getLocalizedMessage());
        }
        return result; //"Failed to fetch data!";
    }

    @Override
    protected void onPostExecute(Integer result) {

        if(pd.isShowing()){
            pd.dismiss();
        }
        /* Download complete. Lets update UI */
        if(result == 1){

            Toast.makeText(BusPassengerInfo.this,"response is reult suceess:"+response,Toast.LENGTH_SHORT).show();

//允许客户去支付网关的方式

        }else{
            Log.e("response", "Failed to fetch data!");
            Toast.makeText(BusPassengerInfo.this,"response is reult fail",Toast.LENGTH_SHORT).show();
        }

    }
}
于 2016-04-24T12:03:54.473 回答