-2

I've done all solutions that I can search. I enabled APIs that are related to my project and made API keys(browser,android) slowly, and surely but not one is working. According to this that my API key is malformed or missing. Is there a way to check if the value in my manifest is changing or getting null when I run my project?

ErlAl
  • 439
  • 1
  • 7
  • 13
  • are you copy and pasting your API keys or typing them out based off of visual? I only ask because, in my experience, I've found that copy and pasting the api keys from the dev dashboard works everytime. – Steve C. Dec 09 '15 at 04:21
  • I tried both copy-pasting and typing, but it still doesn't work. Thanks for the advice, I will try it again. – ErlAl Dec 09 '15 at 04:26

2 Answers2

1

If you are done with Google Setup then just try with this url, It's always working for me...

private String getPlacesApiUrl() {
    String url = "";
    String location = String.valueOf(currentLocation.latitude)+","+String.valueOf(currentLocation.longitude);       
    String radius = "5000";
    String type = "hospital";       
    String sensor = "false";
    String key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

    try {
        url = "https://maps.googleapis.com/maps/api/place/search/json"
            + "?location=" +  URLEncoder.encode(location, "UTF-8")
            + "&type=" + URLEncoder.encode(type, "UTF-8")
            + "&radius=" + URLEncoder.encode(radius, "UTF-8")
            + "&sensor=" + URLEncoder.encode(sensor, "UTF-8")
            + "&key=" + URLEncoder.encode(key, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    Log.d("url",url);
    return url;
}

But make sure you are using Browser Key

Best of luck.

User
  • 4,023
  • 4
  • 37
  • 63
  • 1
    hi thanks for the solution, its working fine now. Do you know how to get the place's long&lat? thanks again sir, you are a life saver – ErlAl Dec 10 '15 at 00:15
  • You're welcome. Please consider "type: parameter in this method. Here you can pass any type listed here https://developers.google.com/places/supported_types. And Please check out this tutorial also https://developers.google.com/places/web-service/search?hl=en. – User Dec 10 '15 at 04:29
0

I solved the problem by using a code that i found from tutorials. hope this help for the people having the same trouble.

I made a PlaceAPI class

public class PlaceAPI{

    private static final String TAG = PlaceAPI.class.getSimpleName();

    private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";

    private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
    private static final String OUT_JSON = "/json";

    private static final String API_KEY = "key here";

    public ArrayList<String> autocomplete (String input) {
        ArrayList<String> resultList = null;
        ArrayList<String> resultListid = null;

        HttpURLConnection conn = null;
        StringBuilder jsonResults = new StringBuilder();

        try {
            StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
            sb.append("?key=" + API_KEY.trim());
            sb.append("&input=" + URLEncoder.encode(input, "utf8"));

            URL url = new URL(sb.toString());
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            // Load the results into a StringBuilder
            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                jsonResults.append(buff, 0, read);
            }
        } catch (MalformedURLException e) {
            Log.e(TAG, "Error processing Places API URL", e);
            return resultList;
        } catch (IOException e) {
            Log.e(TAG, "Error connecting to Places API", e);
            return resultList;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        try {
            // Log.d(TAG, jsonResults.toString());

            // Create a JSON object hierarchy from the results
            JSONObject jsonObj = new JSONObject(jsonResults.toString());
            JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");//result

            // Extract the Place descriptions from the results
            resultList = new ArrayList<String>(predsJsonArray.length());
            for (int i = 0; i < predsJsonArray.length(); i++) {
                resultList.add(predsJsonArray.getJSONObject(i).getString("description"));//geometry
            }
        } catch (JSONException e) {
            Log.e(TAG, "Cannot process JSON results", e);
        }

        //GET PLACEID
        try {

            // Create a JSON object hierarchy from the results
            JSONObject jsonObjid = new JSONObject(jsonResults.toString());
            JSONArray predsJsonArrayid = jsonObjid.getJSONArray("predictions");

            // Extract the Place descriptions from the results
            resultListid = new ArrayList<String>(predsJsonArrayid.length());
            for (int i = 0; i < predsJsonArrayid.length(); i++) {
                resultListid.add(predsJsonArrayid.getJSONObject(i).getString("id"));
            }
        } catch (JSONException e) {
            Log.e(TAG, "Cannot process JSON results", e);
        }

        return resultList;
    }
}

and made an adapter PlacesAutoCompleteAdapter

class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {

    ArrayList<String> resultList;

    Context mContext;
    int mResource;

    PlaceAPI mPlaceAPI = new PlaceAPI();

    public PlacesAutoCompleteAdapter(Context context, int resource) {
        super(context, resource);

        mContext = context;
        mResource = resource;
    }

    @Override
    public int getCount() {
        // Last item will be the footer
        return resultList.size();
    }

    @Override
    public String getItem(int position) {
        return resultList.get(position);
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    resultList = mPlaceAPI.autocomplete(constraint.toString());

                    filterResults.values = resultList;
                    filterResults.count = resultList.size();
                }

                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }
            }
        };

        return filter;
    }
}

then using the adapter in autocomplete:

autocomplete = (AutoCompleteTextView) findViewById(R.id.autocomplete);
        autocomplete.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.autocomplete_list_item));
        autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Get data associated with the specified position
                // in the list (AdapterView)
                place = (String) parent.getItemAtPosition(position);
                //Toast.makeText(this, description, Toast.LENGTH_SHORT).show();
            }
        });
ErlAl
  • 439
  • 1
  • 7
  • 13