0

I have made a demo in android, and I'm making a http request to an API. I have made an asynctask for making http request. When I make the request to the API, the app crashed and in logcat it says "illegalargumentException" in my request. I am not getting why it is happening.

**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..
        }
    }

I receive the illegal argument at this line:

response = httpclient.execute(new HttpGet(uri[0]));
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
John Watson
  • 15
  • 2
  • 8

1 Answers1

0

Looks like you're not executing your AsyncTask (RequestTask) properly (you forgot to include the way you use it btw)

With your current code, You should be invoking it like this new RequestTask().execute(myUrlString);

Alternate Solution

If you're doing that and for some weird reason it's not working, and since you're extending AsyncTask with your RequestTask, you could make sure 100% you are passing the URI by adding a constructor to your class that receives the URL you want to use, something along these lines:

private final String uri;

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

then on your doInBackground method, do:

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

that should work for sure... but pay attention to how you are starting your RequestTask.

Gubatron
  • 6,222
  • 5
  • 35
  • 37