Reputation: 423
It seems like this error message has been posted a lot, but I have not been able to come up with the proper answer.
I am following this tutorial and I can't get the Google Places Info to show up on my screen. I was looking at the LogCat and saw this:
09-20 02:01:32.278: W/System.err(19832): java.lang.IllegalArgumentException: Illegal character in query at index 127: https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=41.6997688,-86.2406069&radius=1000&sensor=true&types=food|bar|store|museum|art_gallery&key=AIzaSyDdMnQpqT9pr-k6VhwesT1OBAg_qkvflxU
09-20 02:01:32.278: W/System.err(19832): at java.net.URI.create(URI.java:727)
09-20 02:01:32.278: W/System.err(19832): at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
09-20 02:01:32.278: W/System.err(19832): at com.mbau.miniproject2.ShowMapActivity$GetPlaces.doInBackground(ShowMapActivity.java:145)
09-20 02:01:32.278: W/System.err(19832): at com.mbau.miniproject2.ShowMapActivity$GetPlaces.doInBackground(ShowMapActivity.java:1)
09-20 02:01:32.278: W/System.err(19832): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-20 02:01:32.278: W/System.err(19832): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-20 02:01:32.278: W/System.err(19832): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-20 02:01:32.278: W/System.err(19832): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-20 02:01:32.278: W/System.err(19832): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-20 02:01:32.278: W/System.err(19832): at java.lang.Thread.run(Thread.java:841)
09-20 02:01:32.388: W/System.err(19832): org.json.JSONException: End of input at character 0 of
09-20 02:01:32.388: W/System.err(19832): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
09-20 02:01:32.388: W/System.err(19832): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
09-20 02:01:32.388: W/System.err(19832): at org.json.JSONObject.<init>(JSONObject.java:154)
09-20 02:01:32.388: W/System.err(19832): at org.json.JSONObject.<init>(JSONObject.java:171)
09-20 02:01:32.388: W/System.err(19832): at com.mbau.miniproject2.ShowMapActivity$GetPlaces.onPostExecute(ShowMapActivity.java:187)
09-20 02:01:32.388: W/System.err(19832): at com.mbau.miniproject2.ShowMapActivity$GetPlaces.onPostExecute(ShowMapActivity.java:1)
09-20 02:01:32.388: W/System.err(19832): at android.os.AsyncTask.finish(AsyncTask.java:631)
09-20 02:01:32.388: W/System.err(19832): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-20 02:01:32.388: W/System.err(19832): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-20 02:01:32.388: W/System.err(19832): at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 02:01:32.388: W/System.err(19832): at android.os.Looper.loop(Looper.java:137)
09-20 02:01:32.388: W/System.err(19832): at android.app.ActivityThread.main(ActivityThread.java:5276)
09-20 02:01:32.388: W/System.err(19832): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 02:01:32.388: W/System.err(19832): at java.lang.reflect.Method.invoke(Method.java:525)
09-20 02:01:32.388: W/System.err(19832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
09-20 02:01:32.388: W/System.err(19832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
09-20 02:01:32.388: W/System.err(19832): at dalvik.system.NativeStart.main(Native Method)
The code I have is literally the EXACT same as provided in the tutorial, with the exception of my Google API key in the URL, my Maps key in the manifest, and my main activity is called ShowMapActivity.
I'm not sure why this isn't working, but I think it has something to do with the URL, which produces the first error.
At the same time, I can't see any invalid character in the URL, and when I copy/paste the URL from LogCat into my browser, it returns a nice JSON file.
Any thoughts/possible fixes?
Thanks.
Link to my files
Upvotes: 6
Views: 38227
Reputation: 1388
Try using String.valueOf()
when adding the lat & long. It may be leaving extra stuff in there that's causing you trouble when trying to parse it that is getting cut out when the string is being displayed.
String placesSearchStr = new String(
"https://maps.googleapis.com/maps/api/place/nearbysearch/"
+"json?location="
+String.valueOf(lat)
+","
+String.valueOf(lng)
+"&radius=5000&sensor=true"
+"&types=food|bar|store|museum|art_gallery"
+"&key=AIzaSyDdMnQpqT9pr-k6VhwesT1OBAg_qkvflxU"
,"UTF-8");
Upvotes: 0
Reputation: 423
So I ended up changing my code from:
String placesSearchStr="https://maps.googleapis.com/maps/api/place/nearbysearch/"
+"json?location="
+String.valueOf(lat)
+","
+String.valueOf(lng)
+"&radius=5000&sensor=true"
+"&types=food|bar|church|museum|art_gallery"
+"&key=AIzaSyDdMnQpqT9pr-k6VhwesT1OBAg_qkvflxU";
to:
String latVal=String.valueOf(lat);
String lngVal=String.valueOf(lng);
String url;
try {
url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="
+URLEncoder.encode(latVal, "UTF-8")
+","
+URLEncoder.encode(lngVal, "UTF-8")
+"&radius="
+URLEncoder.encode("5000", "UTF-8")
+"&sensor="
+URLEncoder.encode("true", "UTF-8")
+"&types="
+URLEncoder.encode("food|bar|church|museum|art_gallery", "UTF-8")
+"&key="
+URLEncoder.encode("AIzaSyDdMnQpqT9pr-k6VhwesT1OBAg_qkvflxU", "UTF-8");
new GetPlaces().execute(url);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And this solved my problem.
Upvotes: 33
Reputation: 1268
If you would like to use a Java library that handles these HTTP details for you and allows you to interact with the Google Places API through Java methods and objects, you can try Sprockets (disclosure: I'm the developer).
I'm also working on an Android library project that will include Loaders, Adapters, and widgets for use with the Google Places API.
Note that when using Sprockets in Android, you will most likely want to use ProGuard for release builds to strip out the unused code in the dependency libraries (see the POM for dependencies).
Upvotes: 0