2

So im building an android app that gets nearby restaurants using google places api but i have a little problem with the url formatting when i using multiple keywords, when i test the url in the browser it works well but the problem when i fetch the url inside android studio which the part with brackets gets excluded from the url, So all i need is how to put brackets inside the url in android.

this is the main code for url formatting:

String keywordText;
String keyword="(Pizza)and(sushi)";
String BASE_URL="https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
String API_KEY= "AIzaSyBg-iwzAjavEUVV9hOQUr0JljZHL7XFRkQ";
String ApiKey;
String locationText;




public void onLocationComplete(Location location) {
        Log.e("onLocationComlete", keyword);

        keywordText = "&keyword=" + keyword;
        ApiKey = "&key=" + API_KEY;
        radiusText = "&radius=" + radius;
        locationText = "&location=" + location.getLatitude() + "," + location.getLongitude();
        url = BASE_URL + locationText + radiusText + keywordText + ApiKey;

        loc = location;
        new FetchFromServerTask(Restaurants.this, 0).execute(url);


    }

the url should i get after formatting : https://maps.googleapis.com/maps/api/place/nearbysearch/json?&location=33.2711492,35.2125282&radius=10000&keyword=(pizza)and(sushi)&key=AIzaSyBg-iwzAjavEUVV9hOQUr0JljZHL7XFRkQ

but instead i get this not working url from the console :image

is there a way to include brackets inside a url or a way to search places using multiple keywords ??

aoe
  • 87
  • 1
  • 1
  • 10

2 Answers2

0

There are "trusted" parts of your URL and "untrusted" parts. Untrusted parts, such as your search keywords, may come from user interaction and must be encoded so they are interpreted as data. It looks like you are encoding more than should be encoded. Take a look at the accepted answer to this question for a start of a solution.

Community
  • 1
  • 1
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
0

Use Encoder ex:

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

...

String encodedKeyword = null;
try {
          encodedKeyword = URLEncoder.encode(keyword, "UTF-8");
    } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
    }
keywordText = "&keyword=" + encodedkeyword;
  • didn't work this the url i get https://maps.googleapis.com/maps/api/place/nearbysearch/json?&location=33.2710757,35.2125823&radius=10000&keyword=%28pizza%29and%28sushi%29&key=AIzaSyBg-iwzAjavEUVV9hOQUr0JljZHL7XFRkQ – aoe May 18 '16 at 14:46