Java IllegalArgumentException索引处查询中的非法字符

时间:2013-12-22 19:22:19

标签: java php

我正在使用下面的代码,当我使用http://stackoverflow.com/时,它可以正常工作。当我将其更改为http://www.sitetest.com/query.php?request=how are you时,我的应用会抛出异常。它说:

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

那里的非法角色是什么?我无法发现它。

  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

2 个答案:

答案 0 :(得分:1)

似乎你有一些空白角色,而不是常规的 空格字符(十进制32)。此外,它似乎不是URL编码。

答案 1 :(得分:0)

你应该在execute之内,如:

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

所以,完成该行的代码:

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

我的来源:

PHP URL Encoding / Decoding

旁注:

我现在没有在我的机器上安装php环境,需要验证该功能。这就是我找到的。