[英]Why am I getting this error java.lang.IllegalArgumentException?
WHy am I get this error : java.lang.IllegalArgumentException: This consumer expects requests of type org.apache.http.HttpRequest 我为什么会收到此错误: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();
EDIT: Just updating the accepted answer as it is not relevant anymore. 编辑:只是更新已接受的答案,因为它不再相关。 the signpost documentation is a bit outdated and suggest to use CommonsHttpOAuthConsumer in Android due to bugs on HttpURLConnection.
由于HttpURLConnection上的错误,路标文档有点过时并建议在Android中使用CommonsHttpOAuthConsumer。 These have been fixed and now Android removed the Apache HTTP so the correct way to deal with signpost is now via DefaultOAuthConsumer .
这些已经修复,现在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);
It should be obvious in the code you posted that request
is not of type HttpRequest
... 在您发布的代码中应该很明显,
request
不是HttpRequest
类型...
request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
Signpost is trivial to use on android, lol, once you get past the tutorials that are not really up to date, or complete, or in particularly useful order. 路标在android,lol上使用很简单,一旦你超过了那些不是最新,完整或者特别有用的教程。
Anyway here is one way to do this using apache http instead of native android, it's a bit ugly for the sake of brevity but should get you up and running. 无论如何这里有一种方法来使用apache http而不是本机android,它为了简洁起见有点难看,但应该让你运行起来。
Modifed your code a bit to make it work, you probably want to make the HttpClient consistent across calls but I just inlined all that. 修改你的代码以使其工作,你可能想让HttpClient在调用之间保持一致但我只是内联了所有这些。 I also notice you are deserializing the tokens so I am just going to assume that you have the actual OAuth flow working.
我还注意到你正在对令牌进行反序列化,所以我只是假设你有实际的OAuth流程。
Good luck! 祝好运!
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);
That's it 而已
The exception java.lang.IllegalArgumentException is thrown when the method is expecting an argument type and it recieves of another type. 当方法期望参数类型并且它接收到另一种类型时,抛出异常java.lang.IllegalArgumentException。
In this case the method is sign
and the argument is request
: 在这种情况下,方法是
sign
,参数是request
:
consumer.sign(request);
Where it's waiting to recieve HTTPRequest
type and it's recieving another type. 在哪里等待接收
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();
//allowing the customer to going to the payment gate way //允许客户进入支付门
}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.