【发布时间】:2015-03-10 06:22:50
【问题描述】:
我在 android 中做了一个演示,我正在向 API 发出一个 http 请求。我已经为发出 http 请求做了一个异步任务。当我向 API 发出请求时,应用程序崩溃了,并且在 logcat 中它在我的请求中显示“illegalargumentException”。我不明白为什么会这样。
**reqUrl = "http://dev.api.ean.com/ean-services/rs/hotel/v3/itin?_type=json&cid=55505&minorRev=99&apiKey=gs28w3m7nqd8y4gsa4x5qsfs&locale="
+ Consts.Locale
+ "¤cyCode="
+ Consts.currencyCode
+ "&xml=<HotelItineraryRequest><itineraryId>"
+ itrny
+ "</itineraryId><email>"
+ mail
+ "</email></HotelItineraryRequest>";**
异步任务
class RequestTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else {
// Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
// TODO Handle problems..
} catch (IOException e) {
// TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out
.println("::::::::::::::::::MY ITERNARY RESAPONSE::::::::::::::"
+ responseString);
// Do anything with response..
}
}
我在这一行收到非法参数:
response = httpclient.execute(new HttpGet(uri[0]));
【问题讨论】:
-
哪一行你得到非法参数?
-
@RandykaYudhistira- response = httpclient.execute(new HttpGet(uri[0]));
-
你的 uri[0] 为空?用日志检查它
-
尝试
response = httpclient.execute(new HttpGet.setURI(uri[0]));您可能需要创建一个请求变量。更多信息here.
标签: android android-asynctask urlencode illegalargumentexception