2012-04-13 56 views
4

爲什麼我得到這個錯誤:java.lang.IllegalArgumentException異常:該消費者期望類型的請求org.apache.http.HttpRequest爲什麼我得到這個錯誤java.lang.IllegalArgumentException?

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

請求如何創建的? – daveb 2012-04-13 17:38:06

+0

看看我提供的答案(一對夫婦)遇到同樣的問題,概述的解決方案爲我工作。 – Idistic 2012-04-13 22:09:30

回答

5

一旦你通過了不是最新的或完整的教程,或者特別有用的教程,路標在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); 

就是這樣

+0

是否有內置的路標方法來生成簽名? – Fabii 2012-04-14 01:03:45

+0

@Fabii不知道我是否完全明白你在問什麼,sign方法幾乎照顧到你的一切,或者你在尋找一個半自動的幫助方法來自己構建一個簽名,以便你可以修改? (像nonce,令牌等東西) – Idistic 2012-04-17 06:10:29

5

它應該是你張貼request代碼明顯不HttpRequest型的...

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

所以你說這個網站是不正確的? :http://code.google.com/p/oauth-signpost/wiki/GettingStarted – Fabii 2012-04-13 17:47:00

+0

您是否在使用Apache Commons HTTP?你在爲Android編寫這個文件嗎?該文章中有很多警告。這可能是相關的:**如果您需要爲其他HTTP請求類型請求籤名,請查看SupportedHttpLibraries ** – 2012-04-13 18:22:43

+0

@Fabii中的示例 - 是的,那裏的教程沒有顯示使用Apache Http,它是由於Android處理程序中的錯誤,Android應用程序需要它。特拉維斯指出你正確的方向。看看我的答案下面的東西適用於Android和路標 – Idistic 2012-04-13 22:13:25

1

異常java.lang.IllegalArgumentException異常被拋出時的方法需要的參數類型並接受另一種類型。
在這種情況下,該方法是sign和參數是request

consumer.sign(request); 

如果它等待收到HTTPRequest類型和它的recieving另一種類型。

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

    } 
} 
相關問題