强制关闭asynctask(非法字符)

时间:2013-09-06 21:44:38

标签: android android-asynctask

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

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

3 个答案:

答案 0 :(得分:2)

  

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

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

答案 1 :(得分:1)

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

String url = " http://beerportfolio.com/app_getTopTaste.php?t=Rum";
HttpGet request = new HttpGet(url.trim());

答案 2 :(得分: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"}]

正如kabuko所述,角色50在Rum之后。我觉得kabuko是对的。

我猜你在url的末尾有空格。最好对URL进行编码,如下所示。

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

URL encoding in Android