在我的doInBackground中获取IllegalArgumentException

时间:2015-03-10 06:22:51

标签: android android-asynctask urlencode illegalargumentexception

我在android中制作了一个demo,我正在向API发出http请求。我做了一个asynctask来发出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>";**

的AsyncTask

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]));

1 个答案:

答案 0 :(得分:0)

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

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

替代解决方案

如果你这样做并且由于一些奇怪的原因它不起作用,并且由于你使用AsyncTask扩展RequestTask,你可以确保100%你通过添加传递URI您的类的构造函数,它接收您要使用的URL,具体如下:

private final String uri;

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

然后在doInBackground方法上执行:

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

这应该可以肯定......但要注意你如何开始RequestTask