0

我收到此错误,这很奇怪,因为它适用于另一个调用相同异步任务的活动。我无法弄清楚查询中的非法字符是什么:

09-06 17:42:29.098  32101-32497/com.beerportfolio.beerportfoliopro E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
        java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:864)
        Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 50: http://beerportfolio.com/app_getTopTaste.php?t=Rum
        at java.net.URI.create(URI.java:727)
        at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
        at com.example.beerportfoliopro.GetTopTasteBeersJSON.readJSONFeed(GetTopTasteBeersJSON.java:139)
        at com.example.beerportfoliopro.GetTopTasteBeersJSON.doInBackground(GetTopTasteBeersJSON.java:49)
        at com.example.beerportfoliopro.GetTopTasteBeersJSON.doInBackground(GetTopTasteBeersJSON.java:34)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        ... 5 more
4

3 回答 3

2

索引 50 处查询中的非法字符:http ://beerportfolio.com/app_getTopTaste.php?t=Rum

它是索引 50 处的字符,就在“朗姆酒”之后。它可能是某种空白字符。如果您需要更多详细信息,则必须发布有关如何获取/生成该 URL 的代码,但您可能需要添加一些代码以在某处去除空格。

于 2013-09-06T21:49:41.967 回答
1

尝试修剪字符串。它删除了字符串末尾不需要的空格。

String url = " http://beerportfolio.com/app_getTopTaste.php?t=Rum";
HttpGet request = new HttpGet(url.trim());
于 2013-09-06T23:56:21.680 回答
0

试试下面

    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    String url = " http://beerportfolio.com/app_getTopTaste.php?t=Rum";
    HttpGet request = new HttpGet(url);
    HttpResponse response = httpclient.execute(request);
    HttpEntity resEntity = response.getEntity();
    String _response=EntityUtils.toString(resEntity); 
    Log.i(".......",_response);

我刚试过你的网址,我确实得到了回复

09-06 22:05:04.547: I/.......(1460): [{"beer":"Rum Cask","rate":"5","id":"dkRDyR","breweryID":"jC0TAa"}]

正如歌舞伎所说,字符 50 是在朗姆酒之后。我觉得歌舞伎是对的。

我猜你在 url 的末尾有空格。最好将 url 编码如下。

    String query = URLEncoder.encode("Rum  ", "utf-8");
    String url = "http://beerportfolio.com/app_getTopTaste.php?t=" + query;
    HttpGet request = new HttpGet(url);

Android 中的 URL 编码

于 2013-09-06T22:01:02.100 回答