6

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

4
  • What's at line 187 in your code? The source you reference is only ~155 lines. Commented Sep 20, 2013 at 6:45
  • Added my code via a dropbox .zip file line 187 in my code is: places = new MarkerOptions[placesArray.length()]; //marker options for each place returned Commented Sep 20, 2013 at 6:59
  • @Jon To clarify more: line 187 instantiates the MarkerOptions array created with the length of the returned “results” array. Commented Sep 20, 2013 at 7:04
  • @pratik dropbox.com/s/s30kgdt6srtaspz/MiniProject2.zip Commented Sep 20, 2013 at 7:06

3 Answers 3

33

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.

Sign up to request clarification or add additional context in comments.

2 Comments

ahah, I knew it had to be something along those lines. Now I remember someone saying it was a good habit to use "UTF-8", but I've never ran into any issues before that actually required it. Thanks for following up & I've updated my answer with a slightly easier way to set the formatting.
is this working if +12.4242,-98.1739|+12.4298,-98.1739
0

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

Comments

0

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

1 Comment

Thanks for the help. Unfortunately that didn't work. Still getting identical LogCat errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.