0

I am getting this error, which is odd because it works from another activity calling the same async task. I can not figure out what the illegal character is in my query:

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
Mike
  • 6,751
  • 23
  • 75
  • 132

3 Answers3

2

Illegal character in query at index 50: http://beerportfolio.com/app_getTopTaste.php?t=Rum

It's the character at index 50, which is right after "Rum". It's probably some sort of whitespace character. You'll have to post your code for how you get/generate that URL if you want more details, but you might need to add some code to strip whitespace somewhere.

kabuko
  • 36,028
  • 10
  • 80
  • 93
1

try to trim the string. It removes unwanted whitespace at the end of the string.

String url = " http://beerportfolio.com/app_getTopTaste.php?t=Rum";
HttpGet request = new HttpGet(url.trim());
Mark
  • 156
  • 1
  • 7
0

Try the below

    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);

I just tried your url and i do get the response

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

As kabuko stated character 50 is after Rum. I think kabuko is right.

I guess you have space at the end of url. It is better to encode the url as below.

    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

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256