2012-04-13 38 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(); 
     } 

    } 
} 
相关问题