为什么会出现java.lang.IllegalArgumentException错误?
为什么会出现此错误:java.lang.IllegalArgumentException:此使用者需要org.apache.http.HttpRequest类型的请求为什么会出现java.lang.IllegalArgumentException错误?,java,oauth,Java,Oauth,为什么会出现此错误:java.lang.IllegalArgumentException:此使用者需要org.apache.http.HttpRequest类型的请求 CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer (CONSUMER_KEY,CONSUMER_SECRET); consumer.setTokenWithSecret(oaut_token, tokenSecret); U
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();
编辑:
只是更新已接受的答案,因为它不再相关。路标文档有点过时,建议在Android中使用CommonHttpOAuthConsumer,因为HttpURLConnection上有bug。这些已经被修复,现在Android删除了ApacheHTTP,因此处理路标的正确方法现在是通过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);
当方法需要参数类型且接收到另一种类型的参数时,会引发异常java.lang.IllegalArgumentException。
在这种情况下,方法是
符号
,参数是请求
:
consumer.sign(request);
它正在等待接收
HTTPRequest
类型,而它正在接收另一个类型。路标在android上使用非常简单,lol,一旦你通过了那些不是真正最新的、不完整的、或者不是特别有用的教程
无论如何,这里有一种方法可以使用ApacheHTTP而不是本机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);
就是这样那么你是说这个网站是正确的您正在使用ApacheCommonsHTTP吗?你是为Android写这篇文章的吗?那篇文章中有很多警告。这可能与此相关:如果您需要对其他HTTP请求类型的请求进行签名,请查看中的示例SupportedHttpLibraries@Fabii-是的,那里的教程没有显示Apache Http的使用,这是Android应用程序所必需的,因为Android处理程序中有一个bug。特拉维斯给你指出了正确的方向。看看我下面的答案,看看能与android和Signpost@Idistic你的答案现在在上面| o |看看我提供的答案(两人一起)遇到了同样的问题,概述的解决方案对我有效。是否有生成签名的内置路标方法?@Fabii不确定我是否完全理解你的问题,sign方法为您处理几乎所有的事情,或者您是否正在寻找一种半自动的助手方法来自己构造签名,以便您可以修改?(比如nonce、令牌等)我想知道,既然HttpClient在android中已被弃用,HTTPUrlConnection是如何做到这一点的
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();
}
}
}