【问题标题】:Getting IllegalArgumentException in my doInBackground在我的 doInBackground 中获取 IllegalArgumentException
【发布时间】: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
                        + "&currencyCode="
                        + 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


【解决方案1】:

看起来你没有正确执行你的AsyncTask (RequestTask)(顺便说一句,你忘了包括你使用它的方式)

使用您当前的代码,您应该像这样调用它 new RequestTask().execute(myUrlString);

替代解决方案

如果您这样做并且由于某些奇怪的原因它不起作用,并且由于您正在使用您的 RequestTask 扩展 AsyncTask,您可以通过向您的接收您要使用的 URL 的类,类似于以下内容:

private final String uri;

public RequestTask(String uri) {
  this.uri = uri;
}

然后在您的 doInBackground 方法上,执行:

response = httpclient.execute(new HttpGet(this.uri));

这肯定会起作用...但请注意您是如何开始RequestTask的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 2018-02-03
    • 2013-01-03
    • 2021-06-24
    相关资源
    最近更新 更多