簡體   English   中英

為什么我收到此錯誤java.lang.IllegalArgumentException?

[英]Why am I getting this error java.lang.IllegalArgumentException?

我為什么會收到此錯誤: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);

在您發布的代碼中應該很明顯, request不是HttpRequest類型...

request = (HttpURLConnection) url.openConnection();
consumer.sign(request);

路標在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);

而已

當方法期望參數類型並且它接收到另一種類型時,拋出異常java.lang.IllegalArgumentException。
在這種情況下,方法是sign ,參數是request

consumer.sign(request); 

在哪里等待接收HTTPRequest類型並且它正在接收另一種類型。

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

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM