0

I'm using the code below, which works fine when I use http://stackoverflow.com/ . When I change it to http://www.sitetest.com/query.php?request=how are you My app throws an exception. It says:

Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 46: http://www.sitetest.com/query.php?request=how %20are%20you?

What is the Illegal Character in there? I can't spot it.

  AsyncTask<String, String, String> result = new RequestTask().execute("http://www.sitetest.com/query.php?request=how are you"); 
        try { 
            this.textToSpeech(result.get().trim());
        } catch (InterruptedException e) { 
            //e.printStackTrace();
            Toast.makeText(this, "Interrupted",
                    Toast.LENGTH_LONG).show();
        } catch (ExecutionException e) { 
            Toast.makeText(this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
            //e.printStackTrace();
        }
12-22 19:17:32.547: E/AndroidRuntime(20764): FATAL EXCEPTION: AsyncTask #1
12-22 19:17:32.547: E/AndroidRuntime(20764): java.lang.RuntimeException: An error occured while executing doInBackground()
12-22 19:17:32.547: E/AndroidRuntime(20764):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at java.lang.Thread.run(Thread.java:856)
12-22 19:17:32.547: E/AndroidRuntime(20764): Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 46: http://www.sitetest.com/query.php?request=how %20are%20you?
12-22 19:17:32.547: E/AndroidRuntime(20764):    at java.net.URI.create(URI.java:727)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at com.sitetest.chat.MainActivity$RequestTask.doInBackground(MainActivity.java:170)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at com.sitetest.chat.MainActivity$RequestTask.doInBackground(MainActivity.java:1)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-22 19:17:32.547: E/AndroidRuntime(20764):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-22 19:17:32.547: E/AndroidRuntime(20764):    ... 4 more
mico
  • 12,730
  • 12
  • 59
  • 99
user3097794
  • 1
  • 1
  • 2
  • Please post the full stack trace. – Sotirios Delimanolis Dec 22 '13 at 19:28
  • 1
    You need to encode your url first. Check a similar question here: http://stackoverflow.com/questions/724043/http-url-address-encoding-in-java – MillaresRoo Dec 22 '13 at 19:30
  • I don't know what makes what is there happening, but main reason of the error is that it has a space before the first %20. All spaces should be encoded to %20 and the result should be free of spaces. Look: http://stackoverflow.com/questions/497908/are-urls-allowed-to-have-a-space-in-them for reference. – mico Dec 22 '13 at 19:30
  • I have tried url encoding but no lock. removing this ?request=how are you will make the program run – user3097794 Dec 22 '13 at 20:05

2 Answers2

1

Seems you have some whitespace character there which is not the regular
space character (decimal 32). Also, it seems it is not URL encoded.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

You should have inside the execute sth like:

urlencode('http://www.sitetest.com/query.php?request=how are you')

So, complete code on that line:

AsyncTask<String, String, String> result = new RequestTask().execute(urlencode('http://www.sitetest.com/query.php?request=how are you'));

My source:

PHP URL Encoding / Decoding

Side note:

I don't have a php environment installed on my machine right now, the verifying of that function needs to be made by you. That's what I found.

Community
  • 1
  • 1
mico
  • 12,730
  • 12
  • 59
  • 99